0

i want to set the size of the frame with the WIDTH and the HEIGHT declared in the enum in

setSize(dimension.getValue(), dimension.getValue());

but when i made a test i receive in output not the enum values but the enum cardinal orders in this case {1,2}. how i have to change my code to return the correct values?

public enum Dimension {

    WIDTH(700), HEIGHT(400);
    private final int value;

    private Dimension(int value) {
    this.value = value;
    }
    public int getValue(){
    return value;

   }
  }




public class MainFrame extends JFrame {
     private Dimension dimension;
     public static void main(String[] args) {
         final MainFrame mainFrame = new MainFrame();
         mainFrame.setVisible(true);
}
public MainFrame() {
    initGameFrame();
}

private void initGameFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(dimension.getValue(), dimension.getValue());
    add(gamePanel);
    setResizable(false);
    setUndecorated(true);
    pack();
    setLocationRelativeTo(null);
   }
  }
3
  • Suprised that you didn't get any NPE there. Where are you initializing dimension? Commented Mar 15, 2014 at 15:28
  • @RohitJain private Dimension dimension; it is initialize Commented Mar 15, 2014 at 15:33
  • 1
    @CiMat Well, that will be initialized to null. Commented Mar 15, 2014 at 15:36

1 Answer 1

2

Your question heading is:

getting values from enum fields

But that's your problem: You're not getting the values from the enum fields. You should use:

Dimension.WIDTH.getValue()
Dimension.HEIGHT.getValue()

Not,

Dimension.getValue()

which has a different meaning entirely.


As an aside, myself, I would avoid calling setSize(...) anywhere, but rather would use the preferred sizes of my components. If one needs to be exactly specified or specified by a formula, then I'd override its getPreferredSize() method.


As another aside, I would avoid giving classes, interfaces or enums names that clash with core Java class names. For example, I'd rename Dimension to GuiDimension or some-such.


Edit

Note that with this enum:

public enum Fubar {
  FOO, BAR
}

Fubar.FOO and Fubar.BAR are not "static fields". They are constant instances of the enum Fubar. I think that you may be confused on this concept.


Edit 2
You state:

well, i have to explain much better my problem, you don't understand... I know that being enum HEIGHT and WIDTH are not static members, but if you put them in any class and you want these two fields are used by other classes, will surely have to be initialized private static int height = 400 .... to prevent this I adopted the enum.

  1. I still don't see what is stopping you from using the enum instances directly as I posted at the beginning of this answer.
  2. An object's state as defined by the state of its fields, can be shared with other objects without making any fields static. This is what getter methods are for.
  3. You've not mentioned why other classes would need to know the value of these fields. You may have an XY Problem here.

The problem of my question was that i couldnt get access to the values of these enum

Again, this is confusing. The values can be obtained as noted above, Dimension.WIDTH.getValue() and Dimension.HEIGHT.getValue(). Your question suggests that you feel that you must pass in an instance variable and can't use the enums directly, which begs the question of why.

Sign up to request clarification or add additional context in comments.

9 Comments

But OP is using dimension.getValue(). Had he used Dimension.getValue() he would have got compiler error.
exactly this >Dimension.WIDTH.getValue() works, i adpoted this strategy to avoid using static fields.
@CiMat: please clarify your problem if you still have one, and correct your code.
@HovercraftFullOfEels my "problem" is that i don't want to use static field for HEIGHT and WIDTH,these variables are used by many classes ,someone tells me to use the enum to avoid this problem.And now i posted this question. is it correct this way adopted?
@CiMat: Wha? You are not using a static field when you use enum instances. That comment makes no sense. Use the enum instances. That's what they're for, and that's the correct way to use them.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.