Mega Code Archive

 
Categories / Java Tutorial / Class Definition
 

Define the static member

You can use the keyword static in front of a field or method declaration. 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;     } } From inside a static method, you cannot call instance methods or instance fields because they only exist after you create an object. You can access other static methods or fields from a static method. You can only declare a static variable in a class level. You cannot declare local static variables even if the method is static.