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 done 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?
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:
Now you can do:
Then later on you can do:
and name won’t be null becuase you are using the same instance from before.