Java基础复习 Day10
1. static关键字
-
类中static关键字的概述:由于在类中的属性,某些属性是每个对象特有的属性,每个对象需要有独立的数据所以不设置为static,但当对于某一种属性来说,需要多个对象都共享一份数据的话,那这种情况就需要定义为static关键字了,并且只保存一份,保证所有本类对象都可以共享一份
(图片来自某网课,侵权联系删除)
-
static 修饰类的成员变量
如果一个成员变量用static来修饰,那么这个成员变量不再属于某对象而是属于其所在的类,多个对象共享该数据
public class Student { private int id; private String name; private int age; static String room; private static int idCount; public Student(String name, int age) { this.name = name; this.age = age; this.id = ++idCount; } public Student() { this.id=++idCount; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } } public static void main(String[] args) { Student student1 = new Student("Karen",21); student1.room = "Room 1001"; Student student2 = new Student("Kyle",19); System.out.println(student1.getName() + ": " + student1.getAge() + " room: " + student1.room + " id: " + student1.getId()); //Karen: 21 room: Room 1001 id: 1 System.out.println(student2.getName() + ": " + student2.getAge()+ " room: " + student2.room+ " id: " + student2.getId()); //Kyle: 19 room: Room 1001 id: 2 }
-
static修饰成员方法
如果一个成员方法用static来修饰,那么这个成员方法不再属于某对象而是属于其所在的类
并且,没有static修饰方法时,必须先创建对象,通过对象才能访问普通成员方法
而static是属于类的,可以不用创建对象直接用(类. 静态成员方法)来访问
-
访问static成员方法,成员变量
虽然是都可以通过对象.来访问,但是不推荐(即使是对象.来访问,再编译后也是会编译成类.
推荐使用类名称. 静态成员
public class MyClass { public void method1(){ System.out.println("this is a normal member method"); }; public static void method2(){ System.out.println("this is a static member method"); }; } public static void main(String[] args) { MyClass.method2();//this is a static member method MyClass myClass1 = new MyClass(); myClass1.method1();//this is a normal member method myClass1.method2();//this is a static member method }
-
成员方法的调用
如果在本类中,可以省略类名称,直接方法名调用(虽然编译后还会自己带上类名称)
public class MyClassMain { public static void main(String[] args) { methodStatic(); MyClassMain.methodStatic(); } public static void methodStatic(){ System.out.println("this static method is called!!!"); } }
-
注意事项:
-
静态只能调用静态,不能直接访问非静态
原因:因为内存中先有的静态内容,后有的非静态内容
-
静态方法中不能用this
原因:this代表当前对象,通过谁调用的方法,this就指向谁,但是静态方法和对象无关,只和类有关
-
-
静态成员内存图
(图片来自某网课,侵权联系删除)
-
静态代码块
- 静态代码块的格式:
*静态代码块的格式: * public class 类名称{ * static{ * //静态代码块的内容 * } * }
- 静态代码块的特点:
- 当第一次用到本类时, 静态代码快执行唯一的一次
- 静态总是优先于非静态,所以是先执行静态代码块
public class Person { //静态代码块的特点: // 1. 当第一次用到本类时, 静态代码快执行唯一的一次。 // 2. 静态总是优先于非静态,所以是先执行静态代码块 static { System.out.println("this is static block!!!"); } public Person() { System.out.println("this is constructor without arguments!!!"); } } public static void main(String[] args) { Person person1 = new Person(); //this is static block!!! //this is constructor without arguments!!! Person person2 = new Person(); //this is constructor without arguments!!! }
-
静态代码块的典型用途:
用来一次性给类中的静态变量赋值
2. 匿名对象
-
匿名对象格式:
一般创建标准对象的格式都是 类名称 对象名 = new 类名称();
而匿名对象就是指只有标准对象的等号右边的对象,但是没有对象名称和赋值运算符
new 类名称();
-
注意事项
- 匿名对象只能使用唯一的一次,下次使用已经是另一个对象了
- 使用建议:如果确定有一个对象只需要使用唯一的一次,那么就使用匿名对象
-
匿名对象作为方法的参数
public static void main(String[] args) { int anInt = new Scanner(System.in).nextInt(); System.out.println(anInt); //23//input //23//output method1(new Scanner(System.in)); //SDFSDF//input //SDFSDF//output } private static void method1(Scanner scanner) { String inputStr = scanner.nextLine(); System.out.println(inputStr); }
-
匿名对象作为方法返回值
代码略