Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Getter and Setter Problem

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 access 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.

like image 347
newbie Avatar asked Apr 09 '26 00:04

newbie


1 Answers

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());
    }   
like image 98
John Pickup Avatar answered Apr 10 '26 12:04

John Pickup



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!