<< Chapter < Page Chapter >> Page >

Normally, instance methods belonging to an object have direct access to the instance variables belonging to that object, and to the class variablesbelonging to the class from which that object was instantiated.

(Class methods never have access to instance variables or instance methods.)

Name can be duplicated

However, the name of a method parameter or constructor parameter can be the same as the name of an instance variable belonging to the object or a classvariable belonging to the class. It is also allowable for the name of a local variable to be the same as the name of an instance variable or a class variable.In this case, the local variable or the parameter is said to hide the member variable having the same name.

Reference named this is passed to instance methods

As mentioned above, whenever an instance method is called on an object, a hidden reference named this is always passed to the method. The this reference always refers to the object on which the method was called. This makes it possible for the code in the method to refer back to the object onwhich the method was called.

The reference named this can be used to access the member variables hidden by the local variables or parameters having of the same name.

The sample program named This01

The sample program shown in Listing 1 illustrates the use of the this reference to access a hidden instance variable named myVar and a hidden class variable named yourVar .

Listing 1 . The program named This01 .
/*File This01.java Copyright 2002, R.G.BaldwinIllustrates use of this keyword to access hidden member variables.Tested using JDK 1.4.0 under Win2000 The output from this program is:myVar parameter = 20 local yourVar variable = 1Instance variable myVar = 5 Class variable yourVar = 10**************************************/ class This01 {int myVar = 0; static int yourVar = 0;//Constructor with parameters named // myVar and yourVarpublic This01(int myVar,int yourVar){ this.myVar = myVar;this.yourVar = yourVar; }//end constructor//---------------------------------// //Method with parameter named myVar// and local variable named yourVar void myMethod(int myVar){int yourVar = 1; System.out.println("myVar parameter = " + myVar); System.out.println("local yourVar variable = " + yourVar);System.out.println( "Instance variable myVar = "+ this.myVar); System.out.println("Class variable yourVar = " + this.yourVar);}//end myMethod //---------------------------------//public static void main( String[]args){ This01 obj = new This01(5,10);obj.myMethod(20); }//end main method}//End This01 class definition.

The key points

The key points to observe in the program is Listing 1 are:

  • When the code refers to myVar or yourVar , the reference resolves to either an incoming parameter or to a local variable having thatname.
  • When the code refers to this.myVar or this.yourVar , the reference resolves to the corresponding instance variable and class variablehaving that name.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask