Bradley Bradley - 10 days ago 6
Java Question

method() vs Class.method()?

Sorry if this is a duplicate, searched for about 20 minutes couldn't find the question.

If I am using a method which is inside the same class I am calling it in, should I put the class name or not?

public class ThisClass
{
public static void example() { }

public static void example2()
{
example(); //or
ThisClass.example();
}
}

Answer

You normaly call a method by its name. If it is a regular method (without any special modifier) you call the method of the current instance of your class with "example()" or "this.example()" where "example()" it the short form, cause by default the implementation of your current instance (this) is called.

In case the method is a static one, it is good style to call the method preceded by its declaring class-name

ThisClass.example()