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 For the Nth Time Java Pass By copy? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "For the Nth Time Java Pass By copy?" Watch "For the Nth Time Java Pass By copy?" New topic
Author

For the Nth Time Java Pass By copy?

ali kamran
Greenhorn

Joined: Apr 25, 2010
Posts: 29
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Apr 25, 2010
Posts: 29
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For the Nth Time Java Pass By copy?