Reputation: 385
When trying to initialize the instance variable from constructor by keeping the parameter name as instance variable name. I am receiving the output as 0. instead of the value passed. Kindly explain?
public class Circle
{
int x;
int y;
int radius;
//Constructor with same parameters of field name
public Circle(int x,int y,int radius)
{
x=x;
y=y;
radius=radius;
}
//Overridden to String()
public String toString()
{
return "center("+x+" , "+y+") and radius ("+radius+")";
}
//Main method
public static void main(String[] args)
{
System.out.println(new Circle(5,5,50));
}
}
When i pass values from constructor 5,5,50 to the parameters of constructor. Shouldn't they display the same value.
System.out.println(new Circle(5,5,50)); // Value sent to constructor
public Circle(int x,int y,int radius) // value to be taken by constructor parameters
Upvotes: 0
Views: 286
Reputation: 2288
A Local Variable Shadows An Instance Variable
The local variable in your constructor shadows the instance variable
Whereever you are assigning and reading an instance variable, use this
keyword
Consider the below example
public class Employee{
int id;
String name;
public Employee(int id, String name){
this.id = id; //sets the id to the Employee's id
this.name = name; //sets the name to Employee's name
}
}
What you have done in your constructor will not have any effect. If you are using eclipse you can easily see a warning saying The assignment to variable id has no effect
When you are doing new Circle(5,5,50))
, your constructor is just reassigning the local varaibles and not even touching the instance variable.
But when you are printing the value with return "center("+x+" , "+y+") and radius ("+radius+")";
you are referring to the instance variables. Because there was no shadowing in your toString
method. Thats why you are getting 0s as all the instance variables are initialized with the default values
Upvotes: 0
Reputation: 234875
You need to use this.x = x
etc.
The x
passed as a parameter shadows the field. When this happens, you can use this.x
to denote the field.
Upvotes: 5
Reputation: 115388
Line x=x;
does not have any sense because it assigns value of variable x
into the same variable.
If you have field named x
you should refer to it using this
keyword: this.x = x
. In this case you will assign value of argument x
to member x
.
This however does not mean that you always have to use this
to refer to members. You have to do this if you have local variable with the same identifier it your scope.
Upvotes: 0
Reputation: 36304
This is where you use this
:
public Circle(int x,int y,int radius)
{
this.x=x;
this.y=y;
this.radius=radius;
}
without this
you are actually working only on local variables x,y etc.
Upvotes: 0
Reputation: 394126
The constructor's arguments hide the members of your class.
Change your constructor to:
public Circle(int x,int y,int radius)
{
this.x=x;
this.y=y;
this.radius=radius;
}
Or use different names for the members and the arguments.
Upvotes: 1