• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For the Nth Time Java Pass By copy?

 
ali kamran
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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]
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
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
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
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic