1. Exam Points
Class methodsare declared using thestatickeyword.Call class methods(static) using the class name.Call instance methods(non-static) using an instance name.- An instance method(non-static) can call a static method from the same class directly.
If you call a method of a class by the class name, the method must be static.
2. Knowledge Points
(1) Class Methods (Static Methods)
Class methodsarestatic methods.- A method is declared as a class method by using the keyword
staticin theheaderbefore the method name. - A method is an
instance methodif it isnotdeclared asstatic. - Class methods
are typically called using the class namealong with the dot operator. - Example:
public class Circle { static double pi; // a class variable (类变量-static) double radius; // an instance variable (实例变量-not static) public static void printPi(){ // a class method (类方法) System.out.println(pi); } public void printRadius(){ // an instance method (实例方法) System.out.println(radius); } } // Another class other than the Circle class public class Test{ public static void main(String[] args) { // printPi() is a class method, // so we call it using the class name Circle, // no need to create an instance/object of the Circle class. Circle.printPi(); Circle c1 = new Circle(); c1.radius = 2.0; Circle c2 = new Circle(); c2.radius = 3.0; // printRadius() is an instance method, // so we call it using an instance/object name. c1.printRadius(); c1.printRadius(); } }