Declaring Methods  «Prev 


Invoking static methods in Java

The class name in which a static method is declared may be used in the method invocation. For example, if static method1() is declared in MyClass, then method1() may be invoked as MyClass.method1().

Class Methods

The Java programming language supports static methods as well as static variables. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in
ClassName.methodName(args)

Note: You can also refer to static methods with an object reference as in instanceName.methodName(args) but this is discouraged because it does not make it clear that they are class methods. A common use for static methods is to access static fields.
For example, we could add a static method to the Bicycle class to access the numberOfBicycles static field:

public static int getNumberOfBicycles() {
    return numberOfBicycles;
}

Not all combinations of instance and class variables and methods are allowed:
  1. Instance methods can access instance variables and instance methods directly.
  2. Instance methods can access class variables and class methods directly.
  3. Class methods can access class variables and class methods directly.
  4. Class methods cannot access instance variables or instance methods directly.
  5. Class methods must use an object reference.
Also, class methods cannot use the this keyword as there is no instance for this to refer to.