Bradley Bradley - 2 months ago 46
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


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