开发者

Java Getter and Setter Problem

开发者 https://www.devze.com 2023-02-08 01:48 出处:网络
Good day! I created two classes namely Setting and Game; In my game access the Setting class first. In my setting class, I call the setter method from Game which is .setDifficulty. and assign a val

Good day!

I created two classes namely Setting and Game; In my game access the Setting class first.

In my setting class, I call the setter method from Game which is .setDifficulty. and assign a value to it, example == 2.

public class Setting extends javax.swing.JDialog {

       public Setting (JFrame owner) {
                super(owner, true);
                initComponents();
                setSize(400, 250);
                setLocation(370, 250);
                getContentPane().setBackground(new Color(128, 201, 20));
            }
         private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {                                   
            dispose();
            MainGame m2 = new MainGame(this);
            m2.setDifficulty(jComboBox1.getSelectedIndex());
        }           

Then I access My second CLass which is the game. But I cannot get the value of the difficultLvl outside the setter method. (See my comments on the code)

     public class Game extends javax.swing.JDialog {
        private int difficultLvl = 0;

        public Game(JFrame owner) {
            super(owner, true);
            initComponents();
            setSize(500, 500);
            setLocation(300, 120);
            getContentPane().setBackground(Color.getHSBColor(204, 204, 255));
            System.out.println(difficultLvl);  //SHOULD BE == 2, but == 0;
        }


        public void setDifficulty(int Difficulty) {
            this.difficultLvl = Difficulty;
            System.out.println(difficultLvl); == to 2 which is correct...
        }

The problem is that I cannot access the difficultLvl value outside the setter class... It returns to its default assigned value which on this case is 0. What am I doing wrong? How can acc开发者_如何学编程ess the value inside the setter method. I used this.difficultLvl but with no result. I am just new in java... Please help! Your help would be highly appreciated. Thank you.


Within the constructor of game the 'difficultLvl' member will be zero as that is what it is initialised to - no reason to expect it to be 2. Once constructed you use the setter method to set the value to 2; from then on the value will be 2 until set to something else.

If you were to add a getter method:

public int getDifficulty() {
    return difficultLvl;
}

and call this you'll see the value.

I suspect you don't want to construct a new Game on every mouse click but instead keep one and just call the setter method on mouse click:

   private  MainGame m2 = new MainGame(this);

   public Setting (JFrame owner) {
            super(owner, true);
            initComponents();
            setSize(400, 250);
            setLocation(370, 250);
            getContentPane().setBackground(new Color(128, 201, 20));
        }
     private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {                                   
        m2.setDifficulty(jComboBox1.getSelectedIndex());
    }   


difficultLvl is an instance variable, so it has a value for each instance. Every time you create a new instance of Game, it has its own diffucultLvl initialized to 0. If you set the difficultLvl in one Game, it doesn't change it for other Game instances and it doesn't affect future new Game instances.

private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {                                   
        dispose();
        MainGame m2 = new MainGame(this);
        m2.setDifficulty(jComboBox1.getSelectedIndex());
}      

In this code, you create a game, MainGame m2 = new MainGame(), but it has default difficulty, which is what is printed in the constructor. Next, you set its difficulty level (if you print the difficulty after this, it will be right). Then game is thrown away: it goes out of scope - it was only a local variable.


I see a couple of problems.

First are you sure to instantiate MainGame in the Setter class? Is it a subclass of Game or something different? If the code is correct, difficultLvl' inMainGamehas nothing to do withdifficultLvlinGame` - both are differen classes.

Second if you want the difficulty level for a game, either do it with the constructor:

 public Game(int difficultyLevel) {
   this.difficultyLvl = difficultyLevel;
 }

or with the setter method, but then you set the value after creating the object and, because we all can't look into the future, you'll see nothing but the initial value with your actual code.


You are printing the value in the constructor. At that point the value will be 0. Its only after setDifficulty() is called that the value is set to 2.


Its because you are first creating the MainGame object and the System.out.println is in the constructor. THEN you call the setter to change the value. But the Constructor already printed the initial value (since it came first).

Sollution: The difficultyLevel needs to be a parameter of the Constructor for this to work.

Use a debugger and take a closer look. This is a very basic thing, so it is important to fully understand what is happening here.


This line will call the constructor and create an object with difficultLvl = 0;

MainGame m2 = new MainGame(this);

And after you call

m2.setDifficulty(jComboBox1.getSelectedIndex());

then difficultLvl of m2 will be set to the selected index.


You seem to create an instance of MainGame in your mouse click action handler which in turn get's garbage collected as soon as the method invocation finishes. So your value (=2) is lost since the object containing it is collected. So on another click you create a new instance which has a value (=0) since u initialize it with 0.

private int difficultLvl = 0;

I do not know much about what you want to do but it seems that you want to keep a handle pointing at the game object there somewhere in your app.

0

精彩评论

暂无评论...
验证码 换一张
取 消