1
public final class City
{
public final long id;

public final String name;

public final double latitude;

public final double longitude;

public City(long id, String name, double lat, double lng)
{
    this.id = id;
    this.name = name;
    this.latitude = lat;
    this.longitude = lng;
}

public static City fromJsonObject(JSONObject json) throws JSONException
{
    return new City(json.getLong(WebApi.Params.CITY_ID),
            json.getString(WebApi.Params.CITY_NAME),
            json.getDouble(WebApi.Params.LATITUDE),
            json.getDouble(WebApi.Params.LONGITUDE));
}
}

This is my class. I want to access the values of this class in my another class; how do i do that? (value of latitude, longitude, cityid and city name)

2
  • You haven't initialized your final variables. How is your code getting compiled?? Commented Feb 25, 2012 at 8:21
  • @ShashankKadne Where do you see that the finals are not initialized? Or maybe the OP edited his constructor after your question? Commented Feb 25, 2012 at 9:09

1 Answer 1

3

This can be done with using the instance.member notation: (It's like calling methods, only without the parenthesis ;-)

City c = City.fromJsonObject(...);
system.out.println("City name: " + c.name); // etc.

Because the members are marked final they must be set when declared or in the constructor. In this case they are being set in the constructor which is being called from the factory (static) method. However, because they are final, this would be invalid:

City c = ...;
c.name = "Paulville"; // oops (won't compile)! can't assign to final member

While some might argue to [always] use "getters", since this is an [simple] immutable object, I find it (direct member access) a valid approach. However, one really nice thing about "getters" is that they can be defined in an Interface as they are really just methods. This handy feature could be useful to avoid an ABI-breaking change later, or to enable DI, or allow mocking... but don't over-design :)

(I generally avoid "setters" unless absolutely required... no need to introduce mutable state willy-nilly and, if changing state is required, I like to change it via "actions".)

Happy coding.


Now, assuming this question is about removing the static factory method while still keeping final members, then it can still be done trivially (however, then this introduces a "constructor that can throw an exception") which is a whole different can of worms... anyway, note that the requirement of assigning to the final members in the constructor is still being fullfilled.

// constructors can (ickickly) throw exceptions ...
public City(JSONObject json) throws JSONException
{
    id = json.getLong(WebApi.Params.CITY_ID),
    name = json.getString(WebApi.Params.CITY_NAME),
    latitude = json.getDouble(WebApi.Params.LATITUDE),
    longitude = json.getDouble(WebApi.Params.LONGITUDE));
}

Or perhaps it is desired to keep/utilize the factory method while also providing a constructor overload, but this is really starting to get silly...

public City(JSONObject json) throws JSONException
    : this(City.fromJsonObject(json)) {
    // we do everything in the next constructor, it would be neat if it
    // was possible to do static/local stuff before calling the base constructor
    // (so that we could pass in multiple arguments, but .. not possible in Java)
}

City(City other) {
    // a "copy constructor"
    // just copy over the member values, note no exception from here :)
    // also note that this fulfills the contract of assigning to the final
    // members when used as the base constructor for City(JSONObject)
    id = other.id;
    name = other.name;
    latitude = other.latitude;
    longitude = other.longitude;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting java.nullpoint exception when I am accessing the variables from another class..I just want to use the values.Dont want to modify them.
@Maddy Maybe null was the value assigned (to name, the other fields can't be null as they are primitive types)? That is, can/does getString return null? Or perhaps it's not related to this class at all?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.