aspose file tools*
The moose likes Beginning Java and the fly likes Unable to figure for storing var in the memory 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 » Java » Beginning Java
Bookmark "Unable to figure for storing var in the memory" Watch "Unable to figure for storing var in the memory" New topic
Author

Unable to figure for storing var in the memory

Frank Sundo
Greenhorn

Joined: Jul 02, 2003
Posts: 4
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

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Jul 02, 2003
Posts: 4
Thank you Maulin! Good point!
 
Consider Paul's rocket mass heater.
 
subject: Unable to figure for storing var in the memory