You are accessing two different instances of ScriptVariables so they don't share instance variables. The fact that you have set the text and foodName properties on the first instance does not affect the new instance you create in your other class.

What you can do is either make the text and foodName properties static, or pass a reference to the first ScriptVariables instance to the second piece of code.

Answer from Keppil on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 31881827 › cant-access-getter-in-external-class
java - Can't access getter in external class - Stack Overflow
August 7, 2015 - I am trying to access a getter function from another class after creating it in the main method of a program. I have tried accessing the function from other places, but IntelliJ still gives the same error: cannot resolve method "getCurrentDeviceInfo()" ... public static void main(String[] args) { optionsGUI = new optionsGUI(); currentMidiDeviceInfo = optionsGUI.getCurrentDeviceInfo(); } ... import javax.sound.midi.MidiDevice; import javax.sound.midi.MidiSystem; import javax.swing.*; import java.awt.*; public class optionsGUI extends JFrame{ public MidiDevice.Info currentDeviceInfo; public Midi
🌐
Project Lombok
projectlombok.org › features › GetterSetter
@Getter and @Setter
Legal access levels are PUBLIC, PROTECTED, PACKAGE, and PRIVATE. You can also put a @Getter and/or @Setter annotation on a class.
🌐
DZone
dzone.com › data engineering › data › java getter and setter: basics, common mistakes, and best practices
Best Practices for Java Getter and Setter - DZone
October 1, 2019 - So far, the setter and getter methods protect a variable’s value from unexpected changes by the outside world — the caller code. When a variable is hidden by the private modifier and can be accessed only through getter and setter, it is encapsulated.
🌐
Coderanch
coderanch.com › t › 639635 › java › access-getter-method-class
How to access a getter method from a different class? (Beginning Java forum at Coderanch)
September 16, 2014 - programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... I have 2 classes. TestClassA has 1 getter and 1 setter. From my second class, TestClassB, I want to access the getter method of TestClassA.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 56027992 › expression-language-cant-access-getter-methods
java - Expression language can't access getter methods - Stack Overflow
May 7, 2019 - Both give me an javax.el.PropertyNotFoundException: No public static field named [mail] was found on class [org.isse.sopro.user] error and an java.lang.NoSuchFieldException: mail error. I already tried renaming the getter methods but it did not solv the issue.
🌐
Quora
quora.com › How-do-I-access-a-protected-getter-setter-in-another-package-in-Java
How to access a protected getter setter in another package in Java - Quora
Answer: I’m not entirely sure, since I am a C++ developer, but the only way to access it would be if you: 1) change visibility to public in the class if you have access to the source code 2) derive a class and add public getter and setter. But it is not good practice to have bigger visibility i...
🌐
W3Schools
w3schools.com › java › java_encapsulation.asp
Java Encapsulation and Getters and Setters
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must: ... You learned from the previous chapter that private variables can only be accessed within the same class (an outside class has no access to it).
Top answer
1 of 7
3

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());
    }   
2 of 7
2

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.

🌐
Stack Overflow
stackoverflow.com › questions › 51046637 › getter-not-able-to-access-its-value-after-being-set
java - getter not able to access it's value after being set - Stack Overflow
package package name import java.util.Date; public class User { public User() { super(); } private String firstName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } }
🌐
xperti
xperti.io › home › java getters and setters: basics, common mistakes, and best practices
Getters And Setters In Java: Common Mistakes, And How To Avoid Them?
April 28, 2022 - In short, never return a reference of the original object in the getter method. Instead, just return a copy of the original object. Java offers some mutable data types like Date and Calendar. If their values are reset using a getter method or a reference method, it transforms into a big maintainability issue which is very difficult to debug.