JamoBox
JamoBox

Reputation: 766

What is the most conventional way to change my variable value in Java?

In a class I have something along the lines of the following:

public class MyClass {
    private static boolean running;

    public static void main(String[] args) {
        //setRunning(false);
        //running = false;
    }

    public static void setRunning(boolean running) {
        MyClass.running = running;
    }
}

I was wondering what the most conventional way of changing the value of 'running' is, seen as I have access to using the setter method that I use in other classes, as well (somewhat)direct access to changing the variable value without calling a method.

I understand that simply doing running = false; may be more efficient (correct me if I'm wrong) but I am unsure on what the convention is for a class to change its own local variable where others would use its setter method.

Upvotes: 1

Views: 69

Answers (2)

Blakenator
Blakenator

Reputation: 1

If running is used outside of the class, then it has to be public static boolean running;

Inside of the class, saying running = false would do just fine.

Upvotes: 0

eLim
eLim

Reputation: 54

I don't exactly understand what you're question is. I think you're asking how classes should alter their own variables. If that is the case, classes should not invoke their own Getter or Setter methods for local variables, just accessing the variable directly should suffice.

Edit: this may be a stylistic thing, but I suggest using the "this" keyword in your Setter instead of "MyClass" So instead of MyClass.runner = runner, use this.runner = runner;

Upvotes: 1

Related Questions