1.使用static声明属性
static声明全局属性
2.使用static声明方法
直接通过类名调用
3.注意点
使用static方法的时候,只能访问static声明的属性和方法,而非static声明的属性和方法是不能访问的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.example; 2 3 /** 4 * Created by Y on 16/4/13. 5 */ 6 public class Person { 7 public static String getName() { 8 return name; 9 } 10 11 public static void setName(String name) { 12 Person.name = name; 13 } 14 private static String name = "北京"; 15 16 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.example; 2 3 public class MyClass { 4 public static void main(String[] args){ 5 //1.使用STATIC声明属性:STATIC声明全局属性 6 //2.使用STATIC声明方法:直接通过类名调用 7 //注意点:使用STATIC方法的时候,只能访问STATIC声明的属性和方法,而非STATIC声明的属性和方法是不能访问的 8 Person per1 = new Person(); 9 Person per2 = new Person(); 10 Person per3 = new Person(); 11 per1.setName("大连"); 12 System.out.println("1." + per1.getName()); 13 System.out.println("2."+per2.getName()); 14 System.out.println("3."+per3.getName()); 15 16 } 17 }