Does this Java class make sense?

SONYFX

Senior member
May 14, 2003
403
0
0
Can anyone help me understand the class below? There is a private string, a constructor, and a public method. What I don't get is why is it instantiating inside the class itself? How would I test this class?
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
It's declaring a couple of static public variables named USA and MEX within the class. You can access them from anywhere, anytime by CreateNation.USA CreateNation.MEX. Not sure what you mean by test it.
 

XxPrOdiGyxX

Senior member
Dec 29, 2002
631
6
81
It's self referencing itself. Sort of like a linked list. Not really sure why they would do it in this case...but maybe it's to simulate an enumerated string type. I can't remember if it is true for Java but C# doesn't have built-in string enumeration and that might be what they are going after.
 

XxPrOdiGyxX

Senior member
Dec 29, 2002
631
6
81
I forgot to answer the other part of your question. This is how you would test it.

CreateNation someCountry = new CreateNation("RUS");

if (someCountry.toString() == CreateNation.USA.toString())
{
System.out.println("Not the same country");
}

else
{
System.out.println("Same country");
}

Basically the whole purpose is to avoid using literal string values so that if changes need to be made to the string value it can be done in one section of code and be propogated to any class that uses it. Otherwise, you'd have to change each individual literal string if changes need to be made or declare the same constants in all the classes. A better way to implement this would be to include an equality method so you can compare two objects without using toString().
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Yeah, that's a pretty common way to simulate enumerations, pre 1.5. A common thing to do would be to make sure that CreateNation.USA == CreateNation.USA under any and all circumstances. An easy way to break that is to serialize and deserialize the instance, then you have two of them.

XxPrOdiGyxX, your code above is a problem for a couple of reasons. First, note the private constructor on CreateNation. The whole point is that you cannot (normally) create new instances outside the class declaration itself, so creating "RUS" will not compile. Second, the == operator is dangerous to use here. It would be more consistent if the nation string had been assigned like "RUS".intern(). To test equality you can use == on the object instances themselves (since there's only ever supposed to be one instance of each, but see my first paragraph), by using toString().equals(otherNation.toString()) or, ideally, by implementing equals() on CreateNation itself and comparing the value of nation.
 

XxPrOdiGyxX

Senior member
Dec 29, 2002
631
6
81
Originally posted by: kamper
Yeah, that's a pretty common way to simulate enumerations, pre 1.5. A common thing to do would be to make sure that CreateNation.USA == CreateNation.USA under any and all circumstances. An easy way to break that is to serialize and deserialize the instance, then you have two of them.

XxPrOdiGyxX, your code above is a problem for a couple of reasons. First, note the private constructor on CreateNation. The whole point is that you cannot (normally) create new instances outside the class declaration itself, so creating "RUS" will not compile. Second, the == operator is dangerous to use here. It would be more consistent if the nation string had been assigned like "RUS".intern(). To test equality you can use == on the object instances themselves (since there's only ever supposed to be one instance of each, but see my first paragraph), by using toString().equals(otherNation.toString()) or, ideally, by implementing equals() on CreateNation itself and comparing the value of nation.

Thanks for pointing that out. I wasn't really paying careful attention to the stuff...just kind of jotted stuff down to give him an idea of what is going on. But yea, like he said if the constructor is private then only one instance can be created so you will be fine using the == operator for comparisons.