-2

Can we call static method via a temp variable?

There is a class AA

class AA {
    static void test() {
    }
}


class Main {
    public static void main(String[] args) {
        var  aa = AA;
        aa.test();   // Can we call static method via a  temp vairble ?
    }
}

Why I have the question? Because I must modify all the instance call style to class static style in my refactor work. The method was an instance method before and use a local variable to call it and now it became a static method or class. So I consider if there is way that just modify the variable. Maybe it's naive.

Directly, maybe like this, but not right.

Class<AA> aa = AA;
aa.test();   

I know it's not good way to call static method by a instance as well.

And we know there is a way in Java 8 to refer a function. So is there another more meta capability to refer a literal class?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Victor Choy
  • 4,006
  • 28
  • 35
  • 3
    Surely it would be easier to just try it and see than to ask a question about it... – Daniel McLaury Jan 06 '21 at 04:25
  • 1
    @DanielMcLaury - yes and no. The trouble with the "try it and see" approach is that if you don't understand enough about what is going on, you can choose the wrong example and / or draw the wrong conclusions from the results. (But in this case, "try it and see" would have shown the OP the obvious compilation error. `AA` is not declared as a subtype of `Variable` in the example. Ooops!) – Stephen C Jan 06 '21 at 05:03
  • @StephenC: I suspect that OP actually means `var` rather than `Variable` here. – Daniel McLaury Jan 06 '21 at 05:18
  • Could be. Either way, the current version won't compile. – Stephen C Jan 06 '21 at 05:22
  • There is a pre-existing question that is the same thing : https://stackoverflow.com/q/32039657/217324 – Nathan Hughes Jan 06 '21 at 05:28
  • @DanielMcLaury yes, Here just a var. Why I have the question ? Because I must all the call style to class static in my refactor work. The method was an instance method before and use a local variable to call it. So I consider if there is way that just modify the variable. Maybe it's naive (*^o^*) – Victor Choy Jan 06 '21 at 05:38

2 Answers2

3

Can we call static method via a temp variable ?

Yes, you can. (Modulo the compilation errors!)

But you are probably not doing what you think you are doing with this. And it is a bad idea.

In fact, aa.test() actually means exactly the same thing as writing Variable.test(). That is, the value of aa and its actual type are completely ignored in determining which method is called, and in calling the method1.

The class whose test() method is invoked is resolved to Variable at compile time. Java does not support overriding or dynamic dispatching of static methods.

1 - The JLS states that the expression that computes the reference is evaluated, but the resulting reference is then discarded.


Invoking a static method via an instance variable is bad style, and should be avoided.

Consider this:

class AA {
    static void test() {
        System.out.println("AA");
    }
}

class BB extends AA {
    static void test() {
        System.out.println("BB");
    }
}


class Main {
    public static void main(String[] args) {
        AA aa = new BB();
        aa.test();
    }
}

Q: What is the output?

A: The output is "AA".

Daniel McLaury
  • 4,047
  • 1
  • 15
  • 37
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • The explanation inspires me really. "The class whose test() method is invoked is resolved to Variable at compile time. Java does not support overriding or dynamic dispatching of static methods." – Victor Choy Jan 06 '21 at 05:50
1

Actually, that is not how to call a static method from a class.

If we have class AA :

class AA {
    public AA() {} 

    public static void test() {
        System.out.println("Test");
    }
}

We can call the test method like this:

AA.test();

It is not recommended to call static method from it's instance like this:

AA aa = new AA();
aa.test(); 
dyon048
  • 174
  • 4