1:如果没有static会怎样?
1:定义Person类
1:姓名、年龄、国籍,说话行为
2:多个构造,重载形式体现
2:中国人的国籍都是确定的
1:国籍可以进行显示初始化
class Person { String name; int age; String gender; String country = "CN"; Person() { } Person(String name, int age, String gender, String country) { this.name = name; this.age = age; this.gender = gender; this.country = country; } void speak() { System.out.println("国籍:" + country + " 姓名:" + name + " 性别:" + gender + " 年龄:" + age + " 哈哈!!!"); } }
3:new Person 对象
1:分析内存
2:每个对象都维护实例变量国籍也是。
public class PersonDemo { public static void main(String[] args) { Person p1 = new Person("jack", 20, "男"); p1.speak(); Person p2 = new Person("rose", 18, "女"); p2.speak(); } }
4:内存分析
1:栈,堆、共享区
2:Demo.class加载近共享区
1:Demo类的main方法进栈
2:Person p1=new Person();
1:Person.class 加载进方法区
2:堆内存开辟空间,实例变量进行默认初始化,显示初始化。
3:内存地址传给变量p1,栈和堆建立连接
3:person p2=new Person();
1:堆内存开辟空间,实例变量进行默认初始化,显示初始化。
2:内存地址传给变量p2,栈和堆建立连接
4:如果建立多个Person对象发现问题
1:每个对象都维护有国籍。
5:解决问题,内存优化
1:为了让所有Person对象都共享一个country ,可以尝试将country放入共享区。
2:country变量如何放入共享区?对象如何访问?
1:使用static
2:static
1:为了实现对象之间重复属性的数据共享
3:static使用
1:主要用于修饰类的成员
1:成员变量
1:非静态成员变量:需要创建对象来访问
2:静态成员变量:使用类名直接调用,也可以通过对象访问
public static void main(String[] args) { //访问静态成员 //直接通过类名来调用 String country=Person.country; System.out.println(country); //通过对象.成员的形式访问 Person p1 = new Person("jack", 20, "男"); p1.country="US"; p1.speak(); } class Person { String name; int age; String gender; //static 修饰成员变量 static String country = "CN"; Person() { } Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } void speak() { System.out.println("国籍:" + country + " 姓名:" + name + " 性别:" + gender + " 年龄:" + age + " 哈哈!!!"); } }
2:成员方法
可以使用类名直接调用
1:静态函数:
1:静态函数中不能访问非静态成员变量,只能访问静态变量。
2:静态方法不可以定义this,super关键字.
3:因为静态优先于对象存在.静态方法中更不可以出现this
2:非静态函数:非静态函数中可以访问静态成员变量
class Person { String name; int age; String gender; //static 修饰成员变量 static String country = "CN"; Person() { } Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } //非静态方法 void speak() { //非静态方法可以访问静态成员 System.out.println("国籍:" + country ); System.out.println("国籍:" + country + " 姓名:" + name + " 性别:" + gender + " 年龄:" + age + " 哈哈!!!"); } //静态方法 static void run(){ //静态方法只能访问静态成员变量。 System.out.println("国籍:"+country); //静态方法访问非静态成员变量,编译报错。 System.out.println(" 姓名:" + name); //静态方法中不可以出现this,编译报错 this.speak(); } }
2:细节:
1:静态函数中不能使用非静态变量
2:非静态函数可以访问静态变量
3:为什么静态函数中不能访问非静态成员
1:static修饰的成员在共享区中。优先于对象存在
2:验证
1:使用静态代码块验证
1:静态代码块
static{
静态代码块执行语句;
}
1:静态代码块特点
随着类的加载而加载。只执行一次,优先于主函数。用于给类进行初始化。
public class PersonDemo { public static void main(String[] args) { // 访问静态成员 // 直接通过类名来调用 String country = Person.country; System.out.println(country); // 通过对象.成员的形式访问 Person p1 = new Person("jack", 20, "男"); p1.country = "US"; p1.speak(); } } class Person { String name; int age; String gender; // static 修饰成员变量 static String country = "CN"; static { System.out.println("这是静态代码块"); } { System.out.println("这是构造代码块"); } Person() { System.out.println("无参数构造"); } Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; System.out.println(" 有参数构造"); } // 非静态方法 void speak() { // 非静态方法可以访问静态成员 System.out.println("国籍:" + country); System.out.println("国籍:" + country + " 姓名:" + name + " 性别:" + gender + " 年龄:" + age + " 哈哈!!!"); // 非静态方法可以调用静态方法。 run(); } // 静态方法 static void run() { // 静态方法只能访问静态成员变量。 System.out.println("国籍:" + country); } }
4:static特点
1 随着类的加载而加载,静态会随着类的加载而加载,随着类的消失而消失。说明它的生命周期很长。
2 优先于对象存在。-->静态是先存在,对象是后存在。
3 被所有实例(对象)所共享。
4 可以直接被类名调用
5:静态变量(类变量)和实例变量的区别:
1存放位置
1:类变量随着类的加载而加载存在于方法区中.
2:实例变量随着对象的建立而存在于堆内存中.
2生命周期
1:类变量生命周期最长,随着类的消失而消失.
2:实例变量生命周期随着对象的消失而消失.
6:静态优缺点
1: 优点:对对象的共享数据进行单独空间的存储,节省空间 例如Person 都有
国籍。该数据可以共享可以被类名调
2:缺点:生命周期过长
访问出现局限性。(静态只能访问静态)
7: 什么时候定义静态变量
1:静态变量(类变量)当对象中出现共享数据
例如:学生的学校名称。学校名称可以共享
对象的数据要定义为非静态的存放在对内存中(学生的姓名,学生的年龄)
8:什么时候定义静态函数
如果功能内部没有访问到非静态数据(对象的特有数据。那么该功能就可以定义为静态)
9:静态的应用
自定义数组工具类
/* 定义数组工具类 1:定义一个遍历数组的函数 2:定义一个求数组和的功能函数 1. 遍历 2. 两两相加 3:定义一个获取数组最大值的功能函数 4:定义一个获取数组最大值角标的功能函数 5:定义一个返回指定数在指定数组中包含的角标的功能函数 6:定义一个可以用于排序int数组的函数 1:冒泡 2:选择 定义自己的工具类 */ class Arrays { private Arrays() { } // 1:定义一个遍历数组的函数 public static void print(int[] arr) { for (int x = 0; x < arr.length; x++) { if (x != (arr.length - 1)) { System.out.print(arr[x] + ","); } else { System.out.print(arr[x]); } } } // 2:定义一个求数组和的功能函数 public static int getSum(int[] arr) { int sum = 0; for (int x = 0; x < arr.length; x++) { sum += arr[x]; } return sum; } // 3:定义一个获取数组最大值的功能函数 public static int getMax(int[] arr) { int max = 0; for (int x = 0; x < arr.length; x++) { if (arr[max] < arr[x]) { max = x; } } return arr[max]; } // 4:定义一个获取数组最大值角标的功能函数 public static int getIndexMax(int[] arr) { int max = 0; for (int x = 0; x < arr.length; x++) { if (arr[max] < arr[x]) { max = x; } } return max; } // 5:定义一个返回 指定数在指定数组中包含的角标的功能函数 public static int getIndex(int[] arr, int src) { int index = -1; for (int x = 0; x < arr.length; x++) { if (arr[x] == src) { index = x; } } return index; } // 冒泡 public static void test(int[] arr) { for (int x = 0; x < arr.length - 1; x++) { if (arr[x] > arr[x + 1]) { int temp = arr[x + 1]; arr[x + 1] = arr[x]; arr[x] = temp; } } } // 选择排序 public static void selectSort(int[] arr) { for (int x = 0; x < arr.length - 1; x++) { for (int y = 1 + x; y < arr.length; y++) { if (arr[x] > arr[y]) { int temp = arr[y]; arr[y] = arr[x]; arr[x] = temp; } } } } // 7:定义一个可以将整数数组进行反序的功能函数。 public static void reverseSort(int[] arr) { int start = 0; int end = arr.length - 1; for (int x = 0; x < arr.length; x++) { if (start < end) { int tem = arr[start]; arr[start] = arr[end]; arr[end] = tem; } start++; end--; } } // 折半查找 public static int halfSearch(int key, int[] arr) { int min = 0; int max = arr.length - 1; int mid = 0; while (min < max) { mid = (min + max) / 2; if (key > arr[mid]) { min = mid + 1; } else if (key < arr[mid]) { max = mid - 1; } else { return mid; } } return -1; } } class Demo6 { public static void main(String[] args) { int[] arr = { 3, 4, 5, 2, 3, 7, 4 }; Arrays.print(arr); System.out.println(); Arrays.selectSort(arr); Arrays.print(arr); } }
练习:统计创建对象的人数
class Person { public String name; public int age; static public long all_count; public Person(){ all_count++; } public Person( String name , int age ){ all_count++; this.name = name; this.age = age; } // 统计人数的函数 public long getCount(){ return all_count; } // 应该具备找同龄人的功能 public boolean isSameAge( Person p1 ){ return this.age == p1.age; } } class Demo9 { public static void main(String[] args) { Person p1 = new Person( "jame" , 34 ); Person p2 = new Person( "lucy" , 34 ); Person p3 = new Person( "lili" , 34 ); Person p4 = new Person(); System.out.println( p1.getCount() + " " + p2.getCount() + " " + p3.getCount() ); System.out.println( p1.isSameAge( p2 ) ); System.out.println( p1.isSameAge( p3 ) ); } }