Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Rani Singh   on Jul 16 In Java Category.

  
Question Answered By: Willie Gomez   on Jul 16

OK, I did basically what I had below but had to encaspulate color  in
its own separate class  rather than in the nested  class.

public class Color {

/**
* this is privvy to the instance
*/
private String color;

/**
* the constructor is private - I don't want just anyone creating
* colors
*/
private Color(String c) {
color = c;
}

public static final Color RED;

static {
RED = new Color("RED");
}
}


So now if you want a color:

Color c = Color.RED;

and you can't do (which is what I wanted)
Color c = new Color("RED");

I would have preferred to do this:

Fruit.Color c = Fruit.Color.RED;

but you can't have static members in nested classes.

oh well... there are times I really miss C++ :-)

Share: 

 

This Question has 3 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on creating an instance of a member class - how do you do it? Or get search suggestion and latest updates.


Tagged: