• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Design of a "Country" class.

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've created a class to represent Countries of the world.
Here's a snippet of it.

The question is that I don't want to always have to create
a new Country class everytime - but I require something
like constant values.
e.g. Country c = Country.USA;
How do I achieve this ? Would this be too heavyweight - i.e.
instantiating all these classes up front for few hundred
countries in the world ?
Pho
 
Sheriff
Posts: 16578
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want to use the type-safe enum pattern discussed by Joshua Bloch in his book "Effective Java" and Craig Larman in his book "Java 2 Performance and Idioms". The Color class uses the same pattern.
You will have something like the following:
<pre>
public class Country {
public static final Country USA = new Country("United States", "US");
public static final Country CANADA = new Country("Canada", "CA");
...
private String fullName;
private String isoName;
private Country(String fullName, String isoName) {
this.fullName = fullName;
this.isoName = isoName;
}

...
}
</pre>
------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
And inside of my fortune cookie was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic