开发者

Getter/Setter Problem in Java?

开发者 https://www.devze.com 2023-01-03 15:17 出处:网络
I want to pass the String value between the classes both are same package. so i created the the classs like the code:

I want to pass the String value between the classes both are same package. so i created the the classs like the code:

public class Map_Delegate {
String myLatitude;
String myLongitude;
String myName;
private String TAG = "Map_Delegate";

public String getMyName() {

    return this.myName;
}

public void setMyName(String value) {
    Log.v(TAG, value);
    this.myName = value;
}

public String getMyLatitude() {

    return this.myLatitude;
}

public void setMyLatitude(String value) {
    Log.v(TAG, value);
    this.myLatitude = value;
}

public String getMyLongitude() {

    return this.myLongitude;
}

public void setMyLongitude(String value) {
    Log.v(TAG, value);
    this.myLongitude = value;
}
}

But it can't pass the value. I do开发者_JAVA百科ne like this code to set the value:

Map_Delegate map = new Map_Delegate();
map.setMyName(first_name_temp + " " + last_name_temp);
map.setMyLatitude(homeLatitude_temp);
map.setMyLongitude(homeLongitude_temp);

code to get the value:

   Map_Delegate map = new Map_Delegate();
   name_val = getMyName();
   lat_val = getMyLatitude();
   long_val = getMyLongitude();

Why get the Null value can you Guess? All classes in the same package and public .AnyIdea?


Well, you set the values on one instance of Map_Delegate. Later you make a new, blank instance and retrieve its values. Of course they are null. You need a reference to the same object you filled in earlier, not make a new one.


I'm not sure I entirely understand what you are doing, but from what you wrote it looks like you are creating a new Map_Delegate each time. So you create one object and set the values then you create another object and try to get the values (but by creating a new object the values are initialised to null). You should be calling get on the object you first created, which requires holding a reference to it and passing it around to wherever it is needed.

Alternatively it might be that you are trying to implement the Singleton design pattern. Using the Singleton design pattern means that there will only ever be one instance of a given class so you don't need to pass the reference around you can just get it by calling getInstance again. Basically to achieve singleton for your case you would do:

public class Map_Delegate {
    private static Map_Delegate instance;

    private Map_Delegate() {
         // Private constructor so nobody can create an instance of your class.
    }

    public static Map_Delegate getInstance() {
        if (instance == null) {
            instance = new Map_Delegate();
        }
        return instance;
    }

    // All the rest of your code can go here.
}

Now you can do:

Map_Delegate a = Map_Delegate.getInstance();
a.setMyName("Name");
// Whatever else.

Then later on you can do:

Map_Delegate b = Map_Delegate.getInstance();
String name = b.getMyName();

and name won't be null becuase you are using the same instance from before.


The reason for the null values that you are creating two map_delegate instances. A new instnace has null values for the properties (unless you specify a default value - not the case here.) You set the values on the first instance, but then retrieve from second - the second instance is newly constructed with null values.

I would avoid the singleton pattern. Instead, ensure that your code passes the same instance to the parts that need them. Consider what you want do with the map_delegate, and then add a way to pass in the appropriate delegate - usually as a property or a constructor to the collaborator.


Map_Delegate map = new Map_Delegate();
map.setMyName(first_name_temp + " " + last_name_temp);
map.setMyLatitude(homeLatitude_temp);
map.setMyLongitude(homeLongitude_temp);

String name_val;
String lat_val;
String long_val;

name_val = map.getMyName();//<-Not Null
lat_val  = map.getMyLatitude();//<-Not Null
long_val = map.getMyLongitude();//<-Not Null

map = new new Map_Delegate();//Another memory area is newly generated. 
name_val = map.getMyName();//<- Null
lat_val  = map.getMyLatitude();//<- Null
long_val = map.getMyLongitude();//<- Null
0

精彩评论

暂无评论...
验证码 换一张
取 消