Its illegal in Java to declare the two local variables with the same name inside the same enclosing scopes (same enclosing scopes means inside the same class, same method etc).
Example for illegal program which uses the same name for instance variables and partemertrized local variables of constructor:
class Box( )
{
double width;
double height;
double depth;
Box(double width, double height, double depth)
{
width = width;
height = height;
depth = depth;
}
}
If you clearly observe this program, you can understand that instance variable names marked in blue color and variable names of parameters of Box( ) constructor marked in green color are same. This is illegal and hence this program wont run in Java.
Why the programmer want to give the same name to the parameter variables ?
Its for clarity. If you give 'w' instead of 'width', the code wont be readable and we wont be able to understand what w stands for if we have used w instead of width.
How to overcome this problem? Can we have the same name for instance variable and parameter variables in the above example ?
Yes, we've to use 'this' keyword for instance variables to avoid conflicting from the parameter variables as shown below:
class Box( )
{
double width;
double height;
double depth;
Box(double width, double height, double depth)
{
this.width = width; // we've added 'this' keyword along with dot(.) operator for instance variables
this.height = height; // Now the variable with the same name on the right will be identified by Java as parameter variable of Box( ) constructor
this.depth = depth;
}
}
Lets implement this on Eclipse IDE and find out whether it works or not
1. Launch Eclipse IDE, Create a new Java Project 'Project 002', Create a Box Class and Declare instance variables width, height and depth in Box( ) class as shown below:
5. Create a class named 'ThisDemo' under the Project in which 'Box' class is created as shown below:
Click here to download the project containing 'Box' and 'ThisDemo' classes used in this post. (You can download this project and import into Eclipse IDE on your machine)
Please comment below to feedback or ask questions.
'Method Overloading' concept will be explained in next post.
3 comments:
What is the difference between this and super keyword?
What is the difference between this and super keyword?
Hii minty,
For this keyword above example is perfect.
Super keyword : Super is a reference variable which is used to call the super class member from sub class
please go through the below example
Example : class parent{
int Main=10; // parent class
int Maine=20;
}
public class Child extends parent{
int Main=30; // child class
int Maine=40;
public void method(int Main,int Maine) {
System.out.println(Main+Maine);
System.out.println(this.Main+this.Maine);
System.out.println(super.Main+super.Maine); }
public static void main(String args[]){
new Child().method(50,1000);
}}
Output : 1050 // local variables with in the method
70 // variables with in the class (this)
30 // variables from parent class (super)
Post a Comment