• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Doubt in Constructor

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Nikhil Shah Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I retried, still the same.
 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey i didnt understand why y=y*2 gives result 3???
why not 4???

thanks...
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic