D
D
Dmitry072016-08-11 18:40:39
Java
Dmitry07, 2016-08-11 18:40:39

Why can't the class name be omitted for a static method?

Good day.
There is an example of a method that sorts a list using the static sort() and reverseOrder() methods. They are written with reference to the Collections class:

Collections.sort(copy, Collections.reverseOrder());

If the sort() method is found in many classes and one cannot do without a lookup reference to Collections, then the reverseOrder() method is presented in only one instance. Why, in this case, would the entry:
Collections.sort(copy, reverseOrder());
be incorrect?
Thanks to.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sanan Yuzb, 2016-08-11
@Dmitry07

Because you imported the package java.utilthat contains the class Collections, and when you use it, you specify the name of the entire class. If you want to do as you said, then you need to use a static import. Those. addimport static java.util.Collections.reverseOrder;

A
Alexey, 2016-08-11
@TheKnight

I recommend reading about static import. I think this will help you for your specific task.
If, in essence, to implement your Wishlist, it is necessary to bypass all classes in the classpath during compilation and check for the presence of this function there, and only after that write a call to a specific function into the bytecode.
In addition, it is not clear how to work with collisions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question