Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Where is the method?

I just try to understand some code of an api, by reading the source. Here is a link:

https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/configuration/MemorySection.java

In this class you can find the method public int getInt(String path, int def). This method calls toInt(val). Where can I find this method. As there is no object or class specified such as anObject.toInt(val) or ClassName.toInt(val) the method must be defined in that class or in a superclass, but I cant find it.

My questions: Is that the original source? Can you find it? Where is it?

like image 576
stonar96 Avatar asked Sep 15 '26 19:09

stonar96


2 Answers

The toInt() method comes from the class org.bukkit.util.NumberConversions.

Now, why isn't the class specified and how can this work ? If you look at the imports at the top of the file, you will see this :

import static org.bukkit.util.NumberConversions.*;

This basically means

Make available to me any public static method in in the org.bukkit.util.NumberConversions class.

This is a useful feature of Java when you want to make your code more concise. However, since the class responsible for this method is not immediately obvious, it is better to use it only for widely-user helper methods, such as toInt here.

Another typical example are the JUnit assertions. It is even explained in their javadoc :

These methods can be used directly: Assert.assertEquals(...), however, they read better if they are referenced through static import:

import static org.junit.Assert.*;
   ...
    assertEquals(...);
like image 110
David Avatar answered Sep 17 '26 08:09

David


You can find an import statement with static in the first line of the java file(which is nothing but the static import, imports only the methods of the class).

import static org.bukkit.util.NumberConversions.*;

You can find the method in this link. Just traverse to the class and search.

like image 22
Shriram Avatar answered Sep 17 '26 08:09

Shriram