• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling explicit constructors  RSS feed

 
Karesz Mol
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm new here and to Java too , did a little search but didn't found any related example so I will post mine:


The output:
x = 4, y = 2, z = 2
x = 5, y = 2, z = 6

So my question is why z is 2 and not 3? (in the first example)

If I'm right the first constructor of C2 calls the second constructor adding as attribute y. The second constructor of C2 calls the constructor of C1 initializing x. But still i don't get this result x = 4, y = 2, z = 2 of the z. y will be 2 because it's static final and the x will be initialized in C1.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karesz Mol wrote:
If I'm right the first constructor of C2 calls the second constructor adding as attribute y.


The first constructor of C2 calls the second constructor adding as attribute y passing the value of y as it second parameter. This second parameter has a value of 2.

Karesz Mol wrote:
The second constructor of C2 calls the constructor of C1 initializing x. But still i don't get this result x = 4, y = 2, z = 2 of the z. y will be 2 because it's static final and the x will be initialized in C1.


The second constructor of C2 calls the constructor of C1 initializing x. ... and after it does that, it also sets the value of the second parameter to the z instance variable.

Also, note the ordering... Instance variables are initialized after the super() call of the constructor, but before the rest of the constructor. So, when the constructor sets the z instance variable to 2, the initialization of the z instance variable (which sets it to 3) had already ran.

Henry
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and welcome to the Ranch
 
Karesz Mol
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation, it was confusing for me the first object creation new C2(4);, the order is static after that variables and only after that the constructor are initialized. My question is where he gets that value of 2? from the static final int y =2; ?

Sorry for this newbie scheme but I really wanna understand the mechanism. Thanks for your time and I'm happy to be here.
 
Karesz Mol
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can not edit the post but i saw i forgot to include in the steps the 2 initialization of the static y =2 and the z=3, after coming back from the Step 4 which call the constructor of superclass C1 then it will be initialized that two variables continuing to the Step 5.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The long lines in your code make it hard to read, so I would suggest re-posting your code but formatting the comments on their own line and breaking the lines up into 80 characters each.
 
Karesz Mol
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Step 1. when we create a new object newC2(4); this constructor will be called
Step 2. It will be called the second constructor of C2 by passing the value of x=4 and y=2(from the static y? or what is the passing value of y here?) and the variable z why it get the same value of 2
Step 3. This 2 arguments constructor is called from Step 2, receiving the values of x=4 and y=2, am I right?
Step 4. This calls the constructor of super class C1 which first sets the x to 1 after that the constructor sets the value back to 4
Step 5. Initializing the Static variable y to 2 and the normal variable z to 3
Step 6. Variable z gets the value of 2.

My main question is i wanna to understand the mechanism between step 2 and step 3, when you call another constructor from a constructor, what values are passed and the naming of variables.
From Step 2 to 3 the variable y receiving the value of 2 from the static variable and after that passing that value to z?

Another question: when using this(x,y); to call the 2 args constructor those variables  actual parameters/arguments?
And the prototype of the 2 args constructor C2 (int x, int z){} are formal parameters?

I've attached a screenshot where i set a breakpoint before the Step 4 (calling the superclass constructor), the variable z gets the value 2.
java.png
[Thumbnail for java.png]
Java
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your confusion doesn't seem to have anything to do with constructors per se, but with parameters and arguments.  I'm going to use ordinary methods to explain these and although a constructor isn't a true method, as far as parameters and arguments, it acts the same.

When you declare a method, it can have parameters. Here the method sumInts has parameters a and b.  These act like local variables, that is, their scope is the method.  Said another way, the parameters can only to accessed and are only visible in the method.

If I call the method, like int x = sumInts(1, 2), the one and the two are the arguments of the method. In this particular instance, the sumInts method acts like this:

Now it's exactly the same if I call sumInts like this:

That is, the arguments x and y have no relationship to the parameters a and b.  Since Java always passes by value, the values of x and y are passed to the method, just like when we passed the literal values 1 and 2.

Now to instance variables.  These are variable that are declared (and sometimes initialized) outside of any method declaration:

In this code z is an instance variable.  It's scope is the entire class.  Said another way, it can be accessed and is visible anywhere in the class.  So the z in the main method has a value of three.  And since it's used as an argument in sumInts(1, z), its value is passed to the parameter b.

But what if instance and local variables have the same name?

In the method squareInt, the parameter z hides the value of the instance variable z.  The z in the main method has a value of three which is passed to the method squareInt.  There the parameter z gets the value three.

But what if you want to use the instance variable z in the squareInts method?  Then you use the keyword this.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So now let's go through the steps:

Step 1. when we create a new object new C2(4); this constructor will be called 

Correct.
Step 2. It will be called the second constructor of C2 by passing the value of x=4 and y=2(from the static y? or what is the passing value of y here?) and the variable z why it get the same value of 2 

In this(x, y); x is the local parameter from C2 (int x) and y is the class variable static final int y =2;  Class variables have the keyword static.  Their scope is the entire class, just like instance variables.  The variable z still has a value of three at this point.
Step 3. This 2 arguments constructor is called from Step 2, receiving the values of x=4 and y=2, am I right?

The two-parameter constructor receives the value 4 and 2; the parameter x is local and has a value of 4.  The parameter z is local, hiding the instance variable z, and has a value of 2.
Step 4. This calls the constructor of super class C1 which first sets the x to 1 after that the constructor sets the value back to 4

The class C1 declares and initializes an instance variable x to 1.  The constructor has a parameter x that is passed the value 4.  The instance variable x is then set to 4.
Step 5. Initializing the Static variable y to 2 and the normal variable z to 3

The "normal" variable z is called an instance variable, but yes.
Step 6. Variable z gets the value of 2.

The instance variable z get the value from the local parameter z, which is two.

Is that clearer?
 
Karesz Mol
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the brilliant explanation and for your time Knute Snortum
My confusion was indeed at local and instance variables and the passing value.
Plus I learned a new term hiding the instance variable, I've read about this keyword but it seems it wasn't enough. I thought we use this keyword just to call the same class constructor or to avoid confusion when the parameters have the same name.



In the next example is easy to read it:
So here the parameter v gets to say the value of 5 and will be assigned that value to the instance variable myvalue. So it's the same if I say I hide the instance variable named myvalue? Or again i"m confusing tomato with potato





So is the above example, I'm getting confused because of the naming.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karesz Mol wrote:
So here the parameter v gets to say the value of 5 and will be assigned that value to the instance variable myvalue. So it's the same if I say I hide the instance variable named myvalue? Or again i"m confusing tomato with potato

No, typing this.myvalue doesn't hide anything because there is no local variable called myvalue.  In this situation myvalue and this.myvalue are the same.


So is the above example, I'm getting confused because of the naming.

Here you must use this because there is a parameter called value and a nonlocal variable by the same name.  You will see this kind of method many times.  The meaning is, "Assign the value of the parameter value to the nonlocal variable called value."
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!