Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to figure for storing var in the memory

 
Frank Sundo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I confuse in this issue. Basically I set my color=Red to Class b. Then from class c (Note: I do not want to call from class a directly), I retrieved my color but I got the default color=Blue instead of Red. Any ideas?
Greatly appreciated!
Thank you for helping me anyone!

--------------Class a----------------------
public class a {
public static void main (String str[]) {
bb.setColor("red");
c cc = new c();

System.out.println("My get Color" + cc.get());

}
}
--------------Class b--------------------------
public class b {
public String colorName = "blue";
public void setColor(String b){
colorName = b;
}
public String getColor() {
return colorName;
}
}
--------------Class c----------------------------
public class c {
public String get() {

return new b().getColor();

}
}
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Frank
at line - bb.setColor("red");
the color indeed gets changed to "red" BUT that is in "bb" object reference.
now, at line - return new b().getColor(); line, u r doing new b() which means u created a new object reference of class b right? this creates a "new" object of class b and there the variable colorName is still "blue" because u never called setColor() on that object.
if u do,
b tempB = new b();
tempB.setColor("red");
return tempB.getColor() instead then it would work...
u see my point?
regards
maulin
 
Frank Sundo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Maulin! Good point!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic