Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

this Keyword in Java.

  •   this: This keyword will refer to current (object) instance of the class
  •   Using this keyword we can access instance members of the current class (instance variables and instance methods)
  •   Specify this keyword is optional some time and some time mandatory
  •   If there is no confusion between instance variables and local variables then specifying this keyword is optional. If we don’t specify the this keyword then the compiler will specify this keyword automatically and access instance variable of the current class
  •   If there is confusion between instance variables and local variables then specifying this keyword is mandatory .If we don’t specify this keyword, then the compiler will not specify this keyword instead of access the local variables. If we want to access instance variables then we must implicitly specify this keyword
  • this() : this() will refers to Zero parametrized constructor of the current class
  •  this(….) : this(…) will refers to Parametrized constructor of the current class
  •  Rules for constructors :
  •  Every constructor can invoke at most one constructor of the current class by using this() and this(..)
  • The invocation of the current class constructor by using this()/this(…) must be specified as a first statement
  •   The invocation of the current class constructor by using this()/this(…) cannot form a loop.

this keyword
This keyword refers to instance of the current class
This keyword can be specified only constructor and instance method of the current class
Note: this keyword cannot be in the static methods



This()- This keyword
This will refers to Zero Parameterized constructor of the current class
This() can be specified in any Parameterized constructor of the current class

This(….) This Keyword
This(…) will refers to Parameterized constructor of the current class
This(…) can be specified in the either  Zero Parameterized constructor or other Parameterized constructor of the current class
Note: this()/this(…) are designed for invocation constructor from constructor .this()/this(..) cannot specify in the any method.

Post a Comment

0 Comments