Java – calling static methods on classes?

I have a reference to a class object and sometype has a static method Is there a way to call this method without instantiating sometype? You'd better not escape strong typing

Editor: OK, I screwed up

interface Int{
    void someMethod();
}

class ImplOne implements Int{
    public void someMethod() {
        // do something
    }
}

Class<? extends Int> getInt(){
    return ImplOne.class;
}

In this case, somemethod () cannot be static

Solution

By definition, static methods are called on a class, not on an instance of that class

So if you use:

SomeClass.someStaticMethod()

You did not instantiate (leave the class to load and instantiate the someclass class itself, which is handled by the JVM and beyond your scope)

This is contrary to the conventional method called by the instantiated object:

SomeObject o = someObject; // had to be instantiated *somewhere*
o.someMethod();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>