简体   繁体   中英

how to create object for a class with only parameterized constructor?

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)

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM