0

I am creating a simple GUI game (number guessing) in Java.

Apparently, I have a button called Give Up.

When I click the Give Up button, I want to display the answer on a textarea.

However, the targetNumber variable is declared as private:

public class GameUtility {
    private String targetNumber = "2543";

    //rest of the code
}

class GiveUpButton implements ActionListener { //Inner class
    public void actionPerformed(ActionEvent gEvent) {

        GameUtility utility = new GameUtility();
        textArea.append(utility.targetNumber); //How to access the value of targetNumber?
    }
}

How can I access a value of a private variable?

Kcits970
  • 640
  • 1
  • 5
  • 23
  • A private member or method is accessible only within its own class.You need accessors. The principle behind it is called encapsulation. – Daniel Bubenheim Oct 22 '16 at 09:05
  • When you do not want to create a getter/setter method, you may be interested in using [reflection](http://stackoverflow.com/questions/1196192/how-do-i-read-a-private-field-in-java). Be aware that this is a dirty hack. – Monkey Supersonic Oct 22 '16 at 09:07

3 Answers3

2

The private modifier implies that you don't have access to the property directly. But perhaps more importantly, private implies that you shouldn't have access to the property directly. Create a getter for providing access to external classes:

public class GameUtility {
    private String targetNumber = "2543";

    public String getTargetNumber() {
        return targetNumber;
    }

    //rest of the code
}

class GiveUpButton implements ActionListener {
    public void actionPerformed(ActionEvent gEvent) {
        GameUtility utility = new GameUtility();
        textArea.append(utility.getTargetNumber());
    }
}

See also: Java Documentation on Access Control

nbrooks
  • 18,126
  • 5
  • 54
  • 66
2

To make the state of the managed bean accessible, you need to add setter and getter methods for that state.

Once the setter and getter (accessor) methods have been added, you can update and access the value of the private instance. The code should look like the following example:

public class AccessorExample {
    private String attribute;

    public String getAttribute() {
        return attribute;
    }

    public void setAttribute(String attribute) {
        this.attribute = attribute;
    }
}

Letting access the information inside the private instance from outside of the class, only if they ask through a provided mechanism we will call method. The mechanisms for asking an object to reveal information about itself we can call the getter method (e.g. accessorExample.getAttribute();).

Kaan Burak Sener
  • 974
  • 10
  • 19
  • Is it a good idea to combine the setter/getter methods into one? – Kcits970 Oct 22 '16 at 09:41
  • If you need to update this private instance in time, you need to implement the setter method as well inside the same class. This is related to the encapsulation principle (https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)). – Kaan Burak Sener Oct 22 '16 at 10:14
1

The recommended way is to create appropriate Getters and Setters.

See this post to get more insights as how do getters and setters work?

public class AccessorExample {
    private String attribute;

    public String getAttribute() {
        return attribute;
    }

    public void setAttribute(String attribute) {
        this.attribute = attribute;
    }
}

Most of the IDEs provide support to directly generate getters and setters.

  1. Generate Getters and setters in Netbeans.
  2. Generate Getters and setters in Eclipse.
Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71