Use of static keyword and examples

/*
 * Use of the static keyword
 * 
 * 1.static: static
 * 2.static can be used to modify: attributes, methods, code blocks, internal classes
 * 
 * 3. Use static to modify attributes: static variables (or class variables)
 * 3.1 Attributes are divided into static attributes vs. non-static attributes (instance variables) according to whether static modification is used or not.
 * Instance variables: We have created multiple objects of the class, and each object independently has a set of non -static properties in the class. Static properties. When the
 * non-static property in one of the objects is modified , the same property value in the other objects will not be modified.
 * Static variables: We have created multiple objects of the class, and multiple objects share the same static variable. When a static variable is modified through a certain object, it will cause
 * When other objects call this static variable, it is modified.
 * 3.2 Other descriptions of static modified attributes:
 * ① Static variables are loaded with the loading of the class. It can be called in the way of "class. static variable"
 * ② The static variable is loaded earlier than the object is created.
 * ③ Since the class will only be loaded once, there will only be one copy of static variables in memory: in the static field of the method area.
 *          
 * ④ Class variables Instance variables
 * Class yes no
 * Object yes yes
 *          
 * 3.3 Examples of static properties: System.out; Math.PI;
 * 
 * 4. Use static modification method: static method
 * ① Load with the loading of the class, you can use the "class. static method" method Make a call
 * ② Static method Non-static method
 * Class yes no
 * Object yes yes
 * ③ In a static method, only static methods or properties
 can be called* In a non-static method, either non-static methods or properties can be called, or they can be called Static methods or properties
 * 
 * 5. Static attention points:
 * 5.1 This keyword and super keyword cannot be used in static methods
 * 5.2 About the use of static properties and static methods, everyone understands from the perspective of life cycle .
 *    
 * 6. How to determine whether a property should be declared as static during development?
 *> Attributes can be shared by multiple objects, and will not vary from object to object.
 *> Constants in classes are often declared as static
 * 
 * During development, how to determine whether a method should be declared static?
 *> The method of operating static properties is usually set to static
 *> The method in the tool class is customarily declared as static. For example: Math, Arrays, Collections
 */

public class StaticTest {
	public static void main(String[] args) {
		
		Chinese.nation = "中国";
		
		
		Chinese c1 = new Chinese();
		c1.name = "姚明";
		c1.age = 40;
		c1.nation = "CHN";
		
		Chinese c2 = new Chinese();
		c2.name = "马龙";
		c2.age = 30;
		c2.nation = "CHINA";
		
		System.out.println(c1.nation);
		
		//编译不通过
//		Chinese.name = "张继科";
		
		
		c1.eat();
		
		Chinese.show();
		//编译不通过
//		Chinese.eat();
//		Chinese.info();
	}
}
//中国人
class Chinese{
	
	String name;
	int age;
	static String nation;
	
	
	public void eat(){
		System.out.println("中国人吃中餐");
		//调用非静态结构
		this.info();
		System.out.println("name :" +name);
		//调用静态结构
		walk();
		System.out.println("nation : " + nation);
	}
	
	public static void show(){
		System.out.println("我是一个中国人!");
		//不能调用非静态的结构
//		eat();
//		name = "Tom";
		//可以调用静态的结构
		System.out.println(Chinese.nation);
		walk();
	}
	
	public void info(){
		System.out.println("name :" + name +",age : " + age);
	}
	
	public static void walk(){
		
	}
}
package java5;

public class CircleTest {
	public static void main(String[] args) {
		Circle c1 = new Circle();
		Circle c2 = new Circle();
		Circle c3 = new Circle(3.14);
		
		System.out.println("c1的id:" + c1.getId());
		System.out.println("c2的id:" + c2.getId());
		System.out.println("c3的id:" + c3.getId());
		
		System.out.println("创建的圆的个数:" + Circle.getTotal());
	}
}

class Circle{
	private double radius;
	private int id;
	
	private static int total;  //创建的圆的个数
	private static int init = 1001;
	
	public Circle(){
		id = init++;
		total++;
	}
	
	public Circle(double radius) {
//		id = init++;
//		total++;
		this();
		this.radius = radius;
	}

	public double findArea(){
		return 3.14 * radius * radius;
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}

	public int getId() {
		return id;
	}

	public static int getTotal() {
		return total;
	}
}

Guess you like

Origin blog.csdn.net/qq_43629083/article/details/108967654