Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

For the Nth Time Java Pass By copy?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output ?



Explanation:
Now my understanding is the static int does not come in picture.when new cert10 is created it passes the value to super that modifies it through Grade(int x) method but locally and then when it returns back to Cert(int x) the value is 10 and the sop display value after 10++ which is 11; Correct.
please advise

Issue:/What I expected and what I got :
Now my question is if we were to change the Cert(intx) and Grade(int x) to be and Object as it is a copy is passed so when grade increments this would be reflected as 11 and the final sop would be 12 .So when I changed to Integer I still see same answer 11.

please advise ..


[HENRY: fixed code tags]
 
author
Posts: 23926
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ali kamran wrote:
Issue:/What I expected and what I got :
Now my question is if we were to change the Cert(intx) and Grade(int x) to be and Object as it is a copy is passed so when grade increments this would be reflected as 11 and the final sop would be 12 .So when I changed to Integer I still see same answer 11.



Got the first part. Correct. Can you elaborate what you are trying to say for the second part?

Henry
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think the problem here is of SHADOWING. the static variable with the name "x" is shadowed by a variable with the same name in both the constructors. when grade constructor is run its constructor variable is incremented to 11. and when Cert constructor is run its constructor variable is incremented . the value of static variable is unchanged. you can check this by printing System.out.println(Cert.x) in the main method .
 
ali kamran
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

ali kamran wrote:
Issue:/What I expected and what I got :
Now my question is if we were to change the Cert(intx) and Grade(int x) to be and Object as it is a copy is passed so when grade increments this would be reflected as 11 and the final sop would be 12 .So when I changed to Integer I still see same answer 11.



Got the first part. Correct. Can you elaborate what you are trying to say for the second part?

Henry




Hey Henry and gurpeet thanks for your efforts.what I was trying to state was shouldn't the result change for objects as opposed to primitive.If I modified my code to

<code>
class Grade
{
// Integer x=10;
Grade (dodo d){
System.out.println("inside method"+d.weight);
d.weight++;
System.out.println("inside method"+d.weight);
}
// public Grade(){this(x); x++;}
}
public class Cert extends Grade
{
Cert(dodo d){super(d);
System.out.println("inside cert method"+d.weight);
d.weight++; System.out.println(d.weight);}

public static void main(String [] args)
{
dodo d = new dodo();
d.weight=10;
new Cert(d);
}

}
class dodo
{
static int weight;
}

<code>

I get 12 the expected result but why not for Integer which is also an object.Thanks guys ...
 
Henry Wong
author
Posts: 23926
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ali kamran wrote:
Hey Henry and gurpeet thanks for your efforts.what I was trying to state was shouldn't the result change for objects as opposed to primitive.If I modified my code to





I get 12 the expected result but why not for Integer which is also an object.Thanks guys ...




Well, obviously, not all objects are the same... Integer objects are immutable (which I can only speculate is likely the cause since you didn't show the code). In your case (the code that you did show), not only is your class mutable, but since it holds the value in a static field, changing any instance of that class, will change the value.

Henry
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic