Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Getter and Setter Problem
    primarykey
    data
    text
    <p>Good day!</p> <p>I created two classes namely Setting and Game; In my game access the Setting class first. </p> <p>In my setting class, I call the setter method from Game which is <code>.setDifficulty.</code> and assign a value to it, example == 2.</p> <pre><code>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()); } </code></pre> <p>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)</p> <pre><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... } </code></pre> <p>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 <code>this.difficultLvl</code> but with no result. I am just new in java... Please help! Your help would be highly appreciated. Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload