Introduction to Construction Method, Encapsulation, Keyword (this, static) and Code Block

1. Construction Method

1.1 Difference between Constructing Method and Membership Method

The structural methods are divided into parametric and parametric structures, in which the parametric and parametric methods are the overload relations of the methods.

The constructor is called when initializing the object of a class. It has no return value. The method name is the same as the class name. The member method is called actively by the object. It has return value, which is expressed as the operation behavior of the object.

Simply put, the construction method defines an object and assigns the object attribute value, while the member method is called by the object actively, which is the expression of the concrete behavior of the object.

1.2 Parametric-free Structures

[modifier] class name (){

}

public Dog(){

}

When no parametric construct is defined in the class, the jvm assigns a parametric construct by default.

1.3 PARAMETRIC STRUCTURES

[modifier] class name (Type arg1,Type arg2,... {

// Initialization code

}

public Dog(String name,int health,int love,String strain){
		name = name;
		health = health;
		love = love;
		strain = strain;
}

When instantiating a parametric construction method, values must be passed according to the content of the parametric list.

When a parametric constructor is defined in a class, the jvm will no longer assign parametric constructors by default. Therefore, after a parametric constructor is defined, a parametric constructor should be defined to create objects.

1.4 Object Initialization Memory Graph

When a class is compiled, the jvm loads the bytecode file of the class into the method area, reads the member variables and methods defined in the class, loads the method, and calculates the required memory according to the defined properties. When an object is created by using the new keyword, the jvm will apply for memory in the heap area and initialize it. The memory address of the object is stored in the stack area. When we call the construction method to assign the properties of the object, the object in the heap area will be accessed through the memory address of the stack area.

2. encapsulation

Encapsulation: Privatization of some information of a class. It does not allow direct access to external programs, but implements the operation and access of privatized information through the setters and accessors provided by the class.

Packaging steps

[1] Privatization of attributes

[2] Provide common setters and accessors

[3] Add business validation logic to setters and accessors

public class Dog{
	
	// [1] Privately owned, invisible to the outside world
	private String name;

	// [2] Provide public setters and Getters
	public void setName(String name){
                // [3] Logic Check
		if(name.equals("")){
			System.out.println("Name cannot be empty.");
		}else{
			this.name = name;
		}
	}
	public String getName(){
		return this.name;
	}
	public Dog(){
		
	}

	public Dog(String name){
		this.setName(name);
	}
	
	public void showInfo(){
		System.out.print("My name is"+this.name);
	}
}

3. This keyword

This denotes the object itself (reference). Generally speaking, the object name itself is used outside the class, and the This keyword is used inside the class.

3.1 This calls attributes in this class

public class Dog{
	
	String name;
	int health;
	int love;
	String strain;
    public Dog2(String name,int health,int love,String strain){
	this.name = name;
	this.health = health;
	this.love = love;
	this.strain = strain;  //Call attributes in this class
    }
}  

This is used to access the attributes of this class, which solves the problem that local variables and member variables have the same name.

3.2 This calls methods of this class

public class Dog{
private String name; public void setName(String name){ if(name.equals("")){ System.out.println("Name cannot be empty."); }else{ this.name = name; } } public String getName(){ return this.name; } public Dog(){ } public Dog(String name){ this.setName(name); //Call methods in this class } public void showInfo(){ System.out.print("My name is"+this.name); } } 

3.3 This calls other constructors of this class (the first line should be placed when calling)

public class Dog{
	private String name;
     private String heath; public void setName(String name){ if(name.equals("")){ System.out.println("Name cannot be empty."); }else{ this.name = name; } } public String getName(){ return this.name; }
    
     public void setHeath(String heath){
		if(heath<0){
			System.out.println("Health value should not be 0");
              this.heath=0; }else{ this.heath=heath; } } public String getHeath(){ return this.heath; }
     public Dog(){
} public Dog(String name){ this.setName(name); }
     public Dog(String name){ 
       this(name,health,love); //Call other constructors of this class
        this.setStrain(strain);      
    }

4. static keyword

The static keyword represents static, which can modify variables or methods.

4.1 Static Variables

Static variables are decorated by static, stored in the method area, and can be shared and accessed by objects in the class.

There are two ways of accessing: [1] class name, static variable (recommendation), [2] object, static variable.

public class Car{
	String brand;
	String type;
	float price;
	static int count = 0; //Define a static variable
	public Car(){
		Car.count++;
	}
	public Car(String brand,String type,float price){
		this.brand = brand;
		this.type = type;
		this.price = price;
		Car.count++;
	}
	public void showInfo(){
		System.out.println("Vehicle information:");
		System.out.println("brand:"+this.brand);
		System.out.println("Model:"+this.type);
		System.out.println("Price:"+this.price);
		System.out.println("I am the first."+Car.count+"Car"); //Accessing static variables
	}		
}

4.2 Static Method

static methods are modified and categorized.

[modifier] static return value type method name (arg... {

}  

There are two ways to access: [1] class name. method name (recommended); [2] object. method name.

It should be noted that instance methods can access static members, while static methods cannot access non-static members. (When static members are defined, objects are not yet defined.)

Figure summary:

5. code block

Code blocks are declared by {} and can be divided into common code blocks, static code blocks, constructed code blocks and synchronized code blocks according to their location (I haven't learned yet).

5.1 Common Code Block

Ordinary code blocks generally exist in the definition of methods or classes, methods, etc. Ordinary code blocks form a scope.

public class Test03{	
	public static void main(String[] args){		
		int count_1 = 10;		
		// Ordinary code block
		{
			int count_2 = 20;
			System.out.println("count_1:"+count_1);
			System.out.println("count_2:"+count_2);
		}		
		// Invalid outside the scope of a block of code
		System.out.println("count_2:"+count_2);		
	}
}

5.2 Construct Code Blocks

The construction code block is located in the class. The construction code block is executed before the construction method. Construct an object to execute once.

public class Person{
	String name;
	int age;
	// Construct code blocks
	{
		System.out.println("Construct code blocks");
	}
	public Person(){
		System.out.println("Construction method");
	}
	public Person(String name,int age){
		this.name = name;
		this.age = age;
	}
} //Every time an object is constructed, statements in the construction block are executed.

5.3 Static Code Block

Static code blocks are located in classes, categorized and decorated with static. It is executed when a class is loaded and only once when multiple objects are built.

public class Person{
	String name;
	int age;
	static{
		System.out.println("Static code block");
	} //Static code block
	public Person(){
		System.out.println("Construction method");
	}
	public Person(String name,int age){
		this.name = name;
		this.age = age;
	}
}  //Static code blocks are executed when classes are loaded and only once when multiple objects are built

Summary:

Static code blocks are generally used to initialize static resources, and constructed code blocks are generally used to initialize instance members.

Keywords: Java jvm Attribute

Added by twmcmahan on Fri, 17 May 2019 12:11:28 +0300