File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Constructor" Watch "Doubt in Constructor" New topic
Author

Doubt in Constructor

Nikhil Shah Jain
Ranch Hand

Joined: Apr 21, 2009
Posts: 31


Output is 6



Output is 9

Can anyone please explain why changing constructor to Uber(int x) makes all the difference even though x is never made use of?
Doubt may be silly, sorry if it is.
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
I am sure you didnot compile the program and check.For both the cases the answer is 6 only. The second one you are able to understand . Let me explain the first example


when the Minor class constructor calls the super class constructor (ie) in Uber you have declared a local variable y inside the constructor which shadows the class instance variable. Hence the operation y=y*2 doesnot increment the static instance variable but the local variable. Inside the construcotr after y=y*2 print the value as System.out.println(y) the value printed will be 4. Hope you understand


To err is human,
To forgive is not company policy
Nikhil Shah Jain
Ranch Hand

Joined: Apr 21, 2009
Posts: 31
I retried, still the same.
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
I am sorry I will explain correctly
okay

In the first case Uber (int y) and in the second case Uber (int x)

what happens in the first case is that the y declared in the constructor syntax overrides the variable declared in line 2.So when you execute y=y*2 the variable in line 2 doesnot get executed but the variable in the constructor syntax. (ie) the local variable and not the object instance variable) What you have to understand is that the variables in line 2 and line 3 have the same name they are different.Okay... The local variable inside the constructor overshadows the object instance variable

In the second case it is not so. since you have declared Umer(int x) there is nothing to override the object instance variable(variable declared in line 2) so when the statement y=y*2 gets executed the variable that gets executed is the object instance variable

Hope you understood
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Nikhil L Shah wrote:
Can anyone please explain why changing constructor to Uber(int x) makes all the difference even though x is never made use of?

that is because of "SHADOWING OF THE VARIABLE". please read about this.then you will understand.

Nikhil L Shah wrote:Doubt may be silly.


there is no too simple or short question
Nikhil Shah Jain
Ranch Hand

Joined: Apr 21, 2009
Posts: 31
Got it .For Uber(int y)
y = y*2 apply only to the parameter and y++ to the obj variable.
Hence y= 3 is returned and
adding y = y+ 3 to it gives the output 6;
Thanks a lot.
Laiq Ahmed
Ranch Hand

Joined: Jan 04, 2010
Posts: 44
Nikhil L Shah wrote:Got it .For Uber(int y)
y = y*2 apply only to the parameter and y++ to the obj variable.
Hence y= 3 is returned and
adding y = y+ 3 to it gives the output 6;
Thanks a lot.


Since you dud variable shadowing in the first example, use this operator to refer to the instance variable.

Example:
Uber(int y) { this(); this.y = y * 2; }

Now you are modifying Uber's instance variable.

Cheers
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 187
hey i didnt understand why y=y*2 gives result 3???
why not 4???

thanks...
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
curve karve wrote:hey i didnt understand why y=y*2 gives result 3???
why not 4???

thanks...


class Uber {
static int y = 2;
Uber(int y) { this(); y = y * 2; } //difference is here
Uber() { y++; }
}

Because the y in y=y*2 is not the same y that is in y++. Since the method declares Uber(int y), all the interior stuff done to y is to the y variable in the parameter list. Once the method is done, that y is gone, and the y that equals 2 is then incremented, making it 3.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
 
jQuery in Action, 2nd edition
 
subject: Doubt in Constructor