--
Using this Keyword Within member methods or constructors of an instance Java object, this is a reference to the currently executing object on which the method or constructor was invoked. It can not appear within static methods. You can refer to any member of the current object from within an instance method or a constructor by using this:
- Using this keyword as a prefix for member variables, When member variables are shadowed by method or constructor parameters.
- From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation, the invocation of another constructor must be the first line in the constructor. For example,
.... public Date(long time) { this.time = time; } public Date(int year, int month, int day) { this(year, month, day, 0, 0, 0); } ...
- Within the scope of a non-static inner class, this refers to the instance of the non-static inner class.
- Within the scope of a non-static inner class, the syntax NameOfOuterClass.this is used to reference the outer class instance. For example
public class Outer { .... public class Inner { private int x; .... public void innerMethod(int x) { this.x = x; //this refers to an instance of Outer.Inner Outer.this.x = x; //Outer.this refers to an instance of Outer } } }
Using super Keyword
The super keyword in a Java instance refers to the superclass of the current object.
- Accessing methods or variables of an instance's superclass members. If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged).
- From within a constructor, you can use the super keyword to invoke a superclass's constructor. Invocation of a superclass constructor must be the first line in the subclass constructor.
For example
class Superclass { int x, y; public Superclass(int x, int y) { this.x = x; this.y = y; } public void printMethod() { System.out.println("Printed in Superclass."); } } public class Subclass extends Superclass { public Subclass(int x, int y) { super(x, y); //call superclass constructor } public void printMethod() { //overrides printMethod in Superclass super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(9, 8); s.printMethod(); } }
With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called.
- Within the scope of a non-static inner class, super refers to the instance super class of the non-static inner class.
- Within the scope of a non-static inner class, the syntax NameOfOuterClass.super is used to reference the instance of super class of the outer class instance.
- For example
class innersuperclass { public void myMethod(int x) { ..... } } class outsuperclass { public void } public class Outer extends outsuperclass{ .... public class Inner extends innersuperclass{ private int x; .... public void myMethod(int x) { super.myMethod(x); //call super class (innnersuperclass) 's Method method Outer.super.myMethod(x); //Outer.super calls super class (outsupperclass)'s myMethod //of an instance of Outer } } }
The keyword this may be used only in the body of an instance method, instance initializer or constructor, or in the initializer of an instance variable of a class. If it appears anywhere else, a compile-time error occurs.
A static initializer and static method are always invoked without an instance object. You can not using the keyword this or the keyword super in the body of a static context, otherwise results in a compile time error
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.
java.lang.Object
does have such a constructor, so if Object
is the only superclass, there is no problem.class Superclass { public Superclass(int m) { .... } public void printMethod() { System.out.println("Printed in Superclass."); } } public class Subclass extends Superclass { public Subclass(int x, int y) { //COMPILER ERROR!!! //Because the super class does not have no-argument constructor this.x = x; this.y = y; } public void printMethod() { //overrides printMethod in Superclass super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(9, 8); s.printMethod(); } }
Do not confuse by default constructor which is automatically generated by the Java compiler for a class that no other constructors have been defined in the class. A constructor without any parameters is known as a default constructor. The default constructor calls the no parameter constructor in the superclass (e.g., super()) and initialize all instance variables to default value depending on their data type. This action taken by a default constructor ensures that the inherited state of the object is initialized properly.
If you define any constructor for your class, no default constructor is automatically created. There is no a default constructor generated by compiler because there is a contructor (
Superclass(int m)
) defined in that class.
Comments
Post a Comment