Define the static member : static Member « Class Definition « Java Tutorial






  1. You can use the keyword static in front of a field or method declaration.
  2. The static keyword may come before or after the access modifier.

These two are correct:

public static int a;
static public int b;

To define a static method in the MathUtil class:

public class MathUtil {
    public static int add(int a, int b) {
        return a + b;
    }
}
  1. From inside a static method, you cannot call instance methods or instance fields because they only exist after you create an object.
  2. You can access other static methods or fields from a static method.
  3. You can only declare a static variable in a class level.
  4. You cannot declare local static variables even if the method is static.








5.12.static Member
5.12.1.Static Members
5.12.2.Define the static member
5.12.3.Demonstrate static variables, methods, and blocks.
5.12.4.Inside main( ), the static method callme( ) and the static variable b are accessed outside of their class.
5.12.5.reference static field after declaration
5.12.6.static section of Initializer
5.12.7.static section Initializer and logic
5.12.8.Initializer for final static field
5.12.9.Demonstrates Static nested Classes
5.12.10.Reference inner static class
5.12.11.Static field, constructor and exception