Choice question:

  1. A (Uncertainty)
  2. D
  3. AC //this cannot be used in the static method, so D error; in the static initialization block, non-static members can not be directly accessed, so A is correct.
  4. BC // non-static methods must be called by object. method name, which can not be called directly, so B error; if inheritance exists in java classes, subclasses can access non-private methods of the parent class, not just the method of this class.
  5. C

Be careful:
6. Static cannot access non-static
7. this cannot be used in static methods. The essence of this is "the address of the created object"!
8. Call (x) directly when calling class methods of this class in class methods. Attention should be paid to non-static methods. Static methods can not be called directly. They should be called by object. method name. For example:

...
public void login() {
        printCompany();
        System.out.println(company); 
        System.out.println("Sign in:" + name);
    }
     
    public static void printCompany() {
//         login(); // / Call non-static members, compilation will error // Static methods can not access non-static
        System.out.println(company);
    }
     
    public static void main(String[] args) {
        User2 u = new User2(101, "Gao Xiao Qi");
        User2.printCompany();   //Call static method format: class name.
        ...

Programming questions:
1.

/**
 * Chapter IV Exercises
 * 
 * @author Wang Li Sheng
 *
 * Topic: Writing Java Programs
 * Used to show the name and age of a Person. Define a human Person. 
 * There should be two private attributes in this class: name and age.
 * Define constructors to initialize data members. The display () method is redefined to print out the name and age.
 * Create an instance of a human in the main method and display the information.
 */
public class Person {
	String name;
	int age;
	
	public  Person(String name,int age){
		this.name = name;
		this.age = age;
	}
	
	public static  void display(Person p){
		System.out.println("Full name:"+p.name);
		System.out.println("Age:"+p.age);
		
	}
	
	public static void main(String[] args){
		Person p=new Person("Mirror month",23);
		
		Person.display(p);
	}
}

Method 1:

package Exercise;
/**
 * Chapter IV Assignments
 * 
 * @author Wang Li Sheng
 *
 * Title: Define a circle class - Circle,
 * Provide an attribute inside the class: radius (r),
 * Two methods are also provided: getArea() and getPerimeter ().
 * The circumference and area of the circle are calculated by two methods and the results are output.
 * Finally, a test class is defined to use the Circle class.
 * 
 */
public class Circle {
	static double r;
	
	public Circle(double r){
		this.r=r;
	}
	
	public static void getArea(Circle cc){
		double s=Math.PI *r*r;    //Call r here, static can not call non-static, so the definition of r above should be added with static restriction.
		System.out.println("The area is:"+s);
	}
	
	public static void getPerimeter(Circle cc){
		double l=2*Math.PI * r;
		System.out.println("Zhou Chang Wei"+l);
	}
	
	public static void main(String[] args){
		Circle cc=new Circle(1.23);
		
		Circle.getArea(cc);
		Circle.getPerimeter(cc);
	}
}

Method 2:

package Exercise;
 /**
  * Chapter IV Exercises
  * 
  * @author Wang Li Sheng
  * 
  * Method 2 of Question 2 of Programming
  * 
  * Title: Define a circle class - Circle,
  * Provide an attribute inside the class: radius (r),
  * Two methods are also provided: getArea() and getPerimeter ().
  * The circumference and area of the circle are calculated by two methods and the results are output.
  * Finally, a test class is defined to use the Circle class.
  */
public class Circle2 {
	double r;
	
	//Construction method
	Circle2(double r){
		this.r = r;
	}
	
	public void getArea(){
		System.out.println("The measure of area:"+Math.PI*r*r);
	}
	
	public void getPerimeter(){
		System.out.println("Perimeter"+2*Math.PI * r);
	}
	
	public static void main(String[] args){
		Circle2 cc=new Circle2(1.23);
		
		cc.getArea();
		cc.getPerimeter();
	}
}

The difference between method 1 and method 2 is as follows:

  1. Method 1 defines method getArea and method getPerimeter using static restriction, indicating that both methods are static methods. Static methods mean that they belong to the whole supermarket (class), not to a trolley (object). It can be understood that the supermarket gives their location information to the trolley (at this time, the trolley loads the address (that is, the object is the address). (That is, static methods belong to the class, the class gives the address of the method to the pair. Elephant. Therefore, when calling them later, you need to use the supermarket name (class name). method (object) to call, but not the trolley name (object name) to call, and to specify the trolley that is in the code:
        ...
        Circle.getArea(cc);
		Circle.getPerimeter(cc);
		...
  1. Method 2 does not use the static definition methods getArea and getPerimeter, so after creating the object, everything in the supermarket belongs to the trolley (everything in the class belongs to the object), so when calling the method, it needs to use the trolley name to call. That is, in the code:
        ...
        cc.getArea();
		cc.getPerimeter();
		...