Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call a static method of another class without using the class name?

Can I call a static method of another class without using the class name (in the same package)? There are similar questions but all answers there use class names.

like image 612
Saurabh Thakur Avatar asked Apr 29 '18 06:04

Saurabh Thakur


People also ask

Can we call static method without class name?

Yes, you can call a static method without mentioning the class name. There's the import static (see JLS 7.5. 4 for exact mechanism), but even without it, if the name can be resolved (see JLS 15.12. 1 for exact mechanism) without fully qualifying the class, it will work.

How do you call a static class from another class?

Calling static methods If a method (static or instance) is called from another class, something must be given before the method name to specify the class where the method is defined. For instance methods, this is the object that the method will access. For static methods, the class name should be specified.

How do you call a static method from another method?

A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name.

Can we call static method with class name?

Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.


2 Answers

Yes. But, you would need to import it. Specifically, import static. Like,

import static com.package.OtherClass.someMethod;

Then you can call someMethod(String) like

someMethod("Like that");
like image 116
Elliott Frisch Avatar answered Sep 22 '22 14:09

Elliott Frisch


It is possible using static imports, however I would like to caution you against using them. Static imports obfuscate where the code lives which makes it harder to understand the code structure. Combined with * imports, humans can no longer determine (without spending a lot of time) the source of the method, although IDEs can.

An example of why it could be bad: let's say you want to see how a problem was solved in a open source project, to get ideas for your own project. And you know what? You can view the code as HTML online. Things are going great! You view the java file you want to see. Then there is this peculiar method "foo". So you search the page for "foo" and there is exactly 1 match (the one you are looking at). There are multiple import static blabla.* lines at top, so that is a dead end. You download the source. Next you do a full text search on the entire project for "foo(" => 5000 matches in 931 files. At which point you no longer have a choice other than loading the project into an IDE if you want to grok the code. You would not have to do any of that if the author would have made it clear where the method lives to begin with. Now, if you do not use * imports, then finding the class is a 2 step process, so it is not nearly as bad. I personally don't use static imports at all. With short yet meaningful names, I find that the explicit type is preferable.

I dislike static imports, because it breaks OO (well, not technically, just conceptually). But this is a personal opinion and the vast majority disagrees with me. So feel free to form your own. The following post has a great discussion on when (not) to use static imports: What is a good use case for static import of methods?

like image 20
Erik Lievaart Avatar answered Sep 25 '22 14:09

Erik Lievaart