Search code examples
javaclassmethodscallsuperclass

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?


Solution

  • 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(...);