This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reference variable code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reference variable code" Watch "Reference variable code" New topic
Author

Reference variable code

Saurabh Verma
Ranch Hand

Joined: Aug 13, 2007
Posts: 38



The output at line 1 is 10.
Can anybody explain the code?
Sivakumar Manikkavasagam
Greenhorn

Joined: Oct 16, 2007
Posts: 15
In Check this Line(Commented) - bb is the new instance of BClass, so it will not pass this 200 to the main program. It will display only 10
Check this program!!!
I think this program will give you the flow of the variable j.
I hope it will be useful for you.


class AClass {
void aMethod(BClass bb) {
bb.j = 10;
System.out.println("bb.j1=" +bb.j);
bb = new BClass();//Check this
bb.j = 200;
System.out.println("bb.j2=" +bb.j);
}
}
class BClass {
int j = 0;
}
public class Test {
public static void main(String[] args) {
int i = 1;
AClass a = new AClass();
BClass b = new BClass();
b.j = 5;
System.out.println("b.j1=" +b.j);
a.aMethod(b);
System.out.println("b.j2=" +b.j);
int j = b.j;
System.out.println("j="+j);//Line 1
}
}

Output
b.j1=5
bb.j1=10
bb.j2=200
b.j2=10
j=10


Thanks & Regards,<br />Sivakumar M
Saurabh Verma
Ranch Hand

Joined: Aug 13, 2007
Posts: 38
Thanks Shivkumar!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reference variable code
 
Similar Threads
Default constructor
Class casting/assigning question - REVISED
Modifier protected does not hide inherited members to (static) code of superclass package Query
Modifier protected does not hide inherited members to (static) code of superclass package
Why does the below code fail to compile?