This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in assigning Objects

 
pramodh somashekara
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody explain how m3.m1 is assigned to Ex1.m4 in the following code?

class Ex1 {
Ex1() { }
Ex1(Ex1 m) {
m1 = m;
}
Ex1 m1;

public static void main(String[] args) {
Ex1 m2 = new Ex1();
Ex1 m3 = new Ex1(m2);
m3.go();
Ex1 m4 = m3.m1;
m4.go();
Ex1 m5 = m2.m1;
m5.go();
}

void go() {
System.out.print("hi ");
}
}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pramodh somashekara:
Can anybody explain how m3.m1 is assigned to Ex1.m4 in the following code?

class Ex1 {
Ex1() { }
Ex1(Ex1 m) {
m1 = m;
}
Ex1 m1;

public static void main(String[] args) {
Ex1 m2 = new Ex1();
Ex1 m3 = new Ex1(m2);
m3.go();
Ex1 m4 = m3.m1;
m4.go();
Ex1 m5 = m2.m1;
m5.go();
}

void go() {
System.out.print("hi ");
}
}


The type of m1 in Ex1 is Ex1. The type of m4 is Ex1.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic