0

I have a method (ex. helpMethod) that appears in many of my project classes and does something with a variable (ex. xVar) present in all of these classes as a private class property. I would like to make this method static in a default class and use it from there. Is it possible to avoid having to pass xVar as an argument to the static implementation?

Something like:

class helpClass {
    static void helpMethod() {
        return ++xVar;
    }
}

class demoClass {
    private int xVar = 0;

    int addToXVar() {
        helpClass.helpMethod();
    }
}

Instead of:

class helpClass {
    static void helpMethod(int xVar) {
        return ++xVar;
    }
}

class demoClass {
    private int xVar = 0;

    int addToXVar() {
        helpClass.helpMethod(xVar);
    }
}
Thanasis
  • 25
  • 3
  • 2
    `static` methods don't have access to instance variables. – Elliott Frisch Jul 04 '18 at 15:21
  • Classes don't have access to private members of other classes (mostly). – Andy Turner Jul 04 '18 at 15:22
  • `void` methods cannot return a value – Sharon Ben Asher Jul 04 '18 at 15:22
  • 2
    Your second exampe will actually do nothing. You never use the return value of your method. so `xVar` will still be its previous value even after calling `addToXVar()` because java is [pass-by-value](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – OH GOD SPIDERS Jul 04 '18 at 15:22
  • 1
    Your second example doesn't work as the `xVar` for the caller wouldn't get incremented. – Peter Lawrey Jul 04 '18 at 15:23
  • 1
    I would start with some thing which works before trying to make it more elegant. – Peter Lawrey Jul 04 '18 at 15:24
  • Please be more careful and correct while posting code examples. Your second example will not even compile. Your helpMethod declared as void cannot return a value. And simply incrementing the parameter value doesn't have any effect on the caller side. – vanje Jul 04 '18 at 15:27
  • @OHGODSPIDERS - unrelated to this question, I figured I'd pass along that you have the *"highest average comment score"* of all SO users for the last 90 days. *(Not that you win anything...)* ([**Source**](https://data.stackexchange.com/stackoverflow/query/870325/good-commenters-users-with-highest-of-comments-upvoted-last-90-days)) – ashleedawg Jul 04 '18 at 15:46

1 Answers1

1

What you can do to avoid having to pass a reference to the demoClass is use a super class.

class helpClass {
    protected int xVar = 0;

    void helpMethod() {
        ++xVar;
    }
}

class demoClass extends helpClass {

    int addToXVar() {
        helpMethod();
    }
}

Or you can use an interface in Java 8+

interface helper {
    int getXVar();
    void setXVar(int xVar);

    default void helpMethod() {
        setXVar(1 + getXVar());
    }
}

class demoClass implements helpClass {
    private int xVar = 0;

    int addToXVar() {
        helpMethod();
    }

    public int getXVar() { return xVar; }
    public void setXVar(int xVar) { this.xVar = xVar; }
}
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075