Book Your slot
X
ONLINE BOOKING
BOOK NOW
OFFLINE BOOKING
Call or WhatsApp 7993732682 (WhatsApp Now), 9177341827 (WhatsApp Now)
search
Menu Login home
  • Questions

  • Library

  • University Updates

  • Informatives

  • Technology Lines

  • Training & Internships

  • X
    Menu
  • Home
  • Privacy Policy
  • Legal Disclaimer
  • Terms & Conditions
  • Return Policy
  • About Us
  • Need any help?? write to us at

    support@engineershub.co

    Follow Us

    X
    LOGIN
    Login to access posts, links, updates, question papers, materials, one liners!
    Use Your Email Address/Mobile and Password to Login
    Forgot Password?
    Not a member? Sign Up
    LOGIN WITH EMAIL/MOBILE
    Forgot Password?
    Go Back
    FORGOT PASSWORD
    Go Back
    RESET PASSWORD
    Go Back
    Continue with LinkedIn
    OR
    Fill Up a Simple Form
    Already a Member? Login
    SIGN UP
    Fill all the below details correctly and click on Next
    Go Back
    EXPLAIN THIS KEYWORD IN JAVASCRIPT? - EngineersHub
    Go Back
    Question
    Venu Gopal reddy Seelam
    3 years ago
    1 Answer(s) posted Write an answer 5185
    Answer
    Read Mode
    Answer posted by Anvesh Kanchibhotla
    3 years ago
    • In java, it is illegal to declare two local variables with the same name inside the same or enclosing scopes.
    • But you can have formal parameters to methods, which overlap with the names of the class' instance variables.
    • this keyword is used to refer to the current object.
    • this can be used to resolve any name collisions that might occur between instance variables and formal variables.
    • When a formal variable has the same name as an instance variable, the formal variable hides the instance variable.
    •  Also used in method chaining and constructor chaining.

    // instance and formal variables are different

    class Box{
      double w=5,h=5,d=5;
      Box(double w1,double h1,double d1){
          w=w1;
          h=h1;
          d=d1;
      }
      double volume(){
          return w*h*d;
      }
     }
     class BoxTest1{
       public static void main(String args[]){
           Box b=new Box(1,2,3);
           System.out.println("Volume is: " b.volume());
      }
     }
    

    Output:
    Volume is: 6.0

    // instance and formal variables are same
     

    class Box{
       double w=5,h=5,d=5;
       Box(double w,double h,double d){
            w=w;
            h=h;
            d=d;
       }
       double volume(){
            return w*h*d;
       }
      }
      class BoxTest2{
        public static void main(String args[]){
             Box b=new Box(1,2,3);
             System.out.println("Volume is: " b.volume());
       }
      }
    

    Output:
    Volume is:125.0
     

    // 'this' hides the instance variables

    class Box{
       double w=5,h=5,d=5;
       Box(double w,double h,double d){
           this.w=w;
           this.h=h;
           this.d=d;
     }
      double volume(){
           return w*h*d;
      }
     }
     class BoxTest2{
       public static void main(String args[]){
          Box b=new Box(1,2,3);
          System.out.println("Volume is: " b.volume());
      }
     }
    

    Output:
    Volume is:6.0

     

    // method chaining

    class Fchain{ 
            int a,b;
            Fchain setValue(int x,int y){
                    a=x;
                    b=y;
                    return this;
            }
            Fchain disp(){
                   System.out.println("a value is:" a);
                   System.out.println("b value is:" b);
                   return this;
            }
     }
     class FchainDemo{
            public static void main(String args[]){
            Fchain f1=new Fchain();
            f1.setValue(10,20).disp().setValue(11,22).disp();
            }
     }
    
    

    //Constructor Chaining

    class Test{
             int a,b,c,d;
             Test(int x,int y){
                       a=x;
                       b=y;
             }
              Test(int x,int y,int z){
                       this(x,y);
                       c=z;
             }
             Test(int p,int q,int r,int s){
                       this(p,q,r);
                       d=s;
             }
              void disp(){
         System.out.println(a %u201D %u201D b %u201D %u201D c %u201D %u201D d);
             }
     }
    
    
     class TestDemo{
          public static void main(String args[]){
              Test t1=new Test(10,20,30,40);
              t1.disp();
              }
     }

    X
    EXPLAIN THIS KEYWORD IN JAVASCRIPT?
    X
    EXPLAIN THIS KEYWORD IN JAVASCRIPT?
    • In java, it is illegal to declare two local variables with the same name inside the same or enclosing scopes.
    • But you can have formal parameters to methods, which overlap with the names of the class' instance variables.
    • this keyword is used to refer to the current object.
    • this can be used to resolve any name collisions that might occur between instance variables and formal variables.
    • When a formal variable has the same name as an instance variable, the formal variable hides the instance variable.
    •  Also used in method chaining and constructor chaining.

    // instance and formal variables are different

    class Box{
      double w=5,h=5,d=5;
      Box(double w1,double h1,double d1){
          w=w1;
          h=h1;
          d=d1;
      }
      double volume(){
          return w*h*d;
      }
     }
     class BoxTest1{
       public static void main(String args[]){
           Box b=new Box(1,2,3);
           System.out.println("Volume is: " b.volume());
      }
     }
    

    Output:
    Volume is: 6.0

    // instance and formal variables are same
     

    class Box{
       double w=5,h=5,d=5;
       Box(double w,double h,double d){
            w=w;
            h=h;
            d=d;
       }
       double volume(){
            return w*h*d;
       }
      }
      class BoxTest2{
        public static void main(String args[]){
             Box b=new Box(1,2,3);
             System.out.println("Volume is: " b.volume());
       }
      }
    

    Output:
    Volume is:125.0
     

    // 'this' hides the instance variables

    class Box{
       double w=5,h=5,d=5;
       Box(double w,double h,double d){
           this.w=w;
           this.h=h;
           this.d=d;
     }
      double volume(){
           return w*h*d;
      }
     }
     class BoxTest2{
       public static void main(String args[]){
          Box b=new Box(1,2,3);
          System.out.println("Volume is: " b.volume());
      }
     }
    

    Output:
    Volume is:6.0

     

    // method chaining

    class Fchain{ 
            int a,b;
            Fchain setValue(int x,int y){
                    a=x;
                    b=y;
                    return this;
            }
            Fchain disp(){
                   System.out.println("a value is:" a);
                   System.out.println("b value is:" b);
                   return this;
            }
     }
     class FchainDemo{
            public static void main(String args[]){
            Fchain f1=new Fchain();
            f1.setValue(10,20).disp().setValue(11,22).disp();
            }
     }
    
    

    //Constructor Chaining

    class Test{
             int a,b,c,d;
             Test(int x,int y){
                       a=x;
                       b=y;
             }
              Test(int x,int y,int z){
                       this(x,y);
                       c=z;
             }
             Test(int p,int q,int r,int s){
                       this(p,q,r);
                       d=s;
             }
              void disp(){
         System.out.println(a %u201D %u201D b %u201D %u201D c %u201D %u201D d);
             }
     }
    
    
     class TestDemo{
          public static void main(String args[]){
              Test t1=new Test(10,20,30,40);
              t1.disp();
              }
     }

    EngineersHub Logo
    x
    Loading...