static:
不能使用this
View Code
public class Person { Person() { System.out.println("---Constructor"); } static { System.out.println("---static"); } public void get() { System.out.println("---get"); } public static void find() { System.out.println("---find"); } public static void main(String[] args) { System.out.println("---not load"); Person p = new Person(); p.get(); p.find(); Person.find(); }}
---static---not load---Constructor---get---find---find
static就好像定义作用域工作在”全局“
View Code
class People { static int i = 0; public People(int b) { i = b; }}public class Person { public static void main(String[] args) { People p1 = new People(1); People p2 = new People(2); People p3 = new People(3); System.out.println("p1" + "---" + p1.i); System.out.println("p2" + "---" + p2.i); System.out.println("p3" + "---" + p3.i); }}
p1---3p2---3p3---3