2

I want to create a static class which works like enumeration, but with string values. Which of the following ways is the safest to extract a full instance of created class?

public class Name 
{

    public static final Name MY_NAME = new Name("Chris", "Doe");

    public String firstName;
    public String lastName;

    public Name(firstname, lastname)
    {
        this.firstName = firstname;
        this.lastName = lastname;
    }
}

OR

public class Name 
{
    public String firstName;
    public String lastName;

    public Name(firstname, lastname)
    {
        this.firstName = firstname;
        this.lastName = lastname;
    }

    public static Name myName()
    {
        return new Name("Chris", "Doe");
    }
}

4 Answers4

6

Safest? I'm not sure what you mean by that.

As far as best-practices go, the second is potentially wasteful, as it will allocate a new instance of Name every time myName() is invoked. The other uses a constant, so it conserves more memory.

All of which is relatively trivial in a small application.

If you're trying to have your class emulate an enum, the constant is certainly the way to go, as the values of an enum are initialized only once.

Just in case you don't know what an enum actually is, here would be a sample implementation of your class as one:

public enum Name {

    MY_NAME ("Chris", "Doe");

    private final String firstName;
    private final String lastName;

    private Name(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

... and you could then simply refer to Name.MY_NAME.

asteri
  • 11,402
  • 13
  • 60
  • 84
  • 1
    Which somewhat begs the question "why not have an enum in the first place", especially when you are hardcoding the values. Even enums can hackily be dynamic in a way. – Rogue Jun 09 '14 at 14:29
  • @Rogue I agree that you might as well just use an `enum`, but I didn't think that was a suitable answer, haha. – asteri Jun 09 '14 at 14:30
  • @JeffGohlke-Nice explanation using `enum` demonstration. – Am_I_Helpful Jun 09 '14 at 14:39
  • 2
    Worth noting that the `private` modifier on the enum constructor is completely optional as enum constructors are implicitly `private` anyway. It's also a compile-time error to try and set it to anything else. – JonK Jun 09 '14 at 14:39
  • @JonK Sure. I just prefer to be very explicit with my access modifiers. :) – asteri Jun 09 '14 at 14:49
  • Oh it's not a criticism, I do exactly the same thing myself - it's just worth mentioning that you don't **have** to do it. :) – JonK Jun 09 '14 at 15:20
0

I would like to go with the first one, because the first one comprises of static final Name MY_NAME whereas in the second case a new Name instance would be returned everytime you call the myName() method resulting in wastage of memory. So, better go with the first one.

So, to extract a full-instance of a created class, you should go with the first one.

Also,talking about enum----in which you hold constant values, your static field final Name MY_NAME instance will serve the purpose, you should stick to the first-declaration----thereby supporting your need of enum as well as not wasting memory!

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
0

go after first method,

create your public static instances of class, make your class final so it cant be extended, and make constructor private, so it cannot be instantiate outside of your class

flaw with second method is, your static method myName each time creates new instance of Name which is unnecessary

user902383
  • 8,420
  • 8
  • 43
  • 63
0

If you only need one object instance with the fixed values why bother having member variables at all? This way is thread safe regardless of how you use the object.

public class Name 
{
    public String getFirstName() {
        return "Chris"
    }

    public String getFirstName() {
        return "Doe"
    }
}

If you must have member variables then:

public class Name 
{
    public final String firstName = "Chris"
    public final String lastName = "Doe"
}

But as others have suggested just use the enum:

public enum Name {
    me("Chris", "Doe");
    private String firstName;
    private String lastName;

    Name(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}
Usman Ismail
  • 17,999
  • 14
  • 83
  • 165