• 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • 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 ?
Posts: 16578
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:
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;

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
    Bookmark Topic Watch Topic
  • New Topic