• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Reference variable code

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



The output at line 1 is 10.
Can anybody explain the code?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saurabh Verma
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Shivkumar!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic