java class and code block

class

* A group of program codes with a certain function is called "class". We can copy the avatars of this class, so that the “objects” that can be used by the avatars. * With a set of functional classes:

clss Mynumber{
	int number;
	}

It can be seen from the observation that the characteristics of the class are:
(1) Use "class" as a pre-prompt.
(2) There is a class name.
(3) Functions with certain significance.
(4) Use curly brackets "{}" as the code block range.

//创建一个类
public class MyNumber {
	int age;
}
//主程序
public class My_information {

	public static void main(String[] args) {
		MyNumber a = new MyNumber();
		a.age=18;
		System.out.println("a.number="+a.age);
	}
}

This shows how to use the new object:

类名.对象名=new 类名();
//完整格式
类名 对象名;
对象名=new 类名();

1. Code block (Block)
1. Each line of code block does not exist at any time, only when it is run.

public class Test {

	public static void main(String[] args) {
		
		{
			int a=6;
			System.out.println("a="+a);
		}
		{
			int b=66;
			System.out.println("b="+b);
		}
	}

}

2. Its function interprets two unrelated code blocks, and can declare variables with the same name.

public class Test {

	public static void main(String[] args) {
		
		{
			int a=6;
			System.out.println("a="+a);
		}
		{
			int a=66;
			System.out.println("a="+a);
		}
	}

}

3. The internal code cannot be used to interpret the external code, otherwise an error will be reported

public class Test {

	public static void main(String[] args) {
		
		{
			int a=6;
			System.out.println("a="+a);
		}
		{
			int a=66;
			System.out.println("a="+a);
		}
		//System.out.println("a="+a);报错
	}

}

Second, the logical relationship between external code and internal code:
1. The internal code block exists with the existence of the external code block.
2. The names of the variables declared in the external code and the internal code block cannot be the same.
3. Internal code blocks can use the declared variables of external code blocks, while external code cannot use the declared variables of internal code blocks.
4. If it is a code block that does not belong to each other, there is no dependency relationship between each other, and the variables declared by each have no conflict even if the names are the same.

Explanation of the 4 points of the appeal:
1. If the code block does not exist, the internal code block will be run without environment. At this time, the internal block cannot exist, so the internal code block exists as the external code block exists.
2. Because the external code block and the internal code block exist at the same time, the system cannot have two memories with the same name at the same moment, otherwise the system does not know which to choose to run, so the external code block and the internal code block must not have the same name variable declaration .
3. When an internal code block exists, its external code block must also exist, so the internal code can use the declared variables of the external code block; and when an external code block exists, some of its internal code block does not necessarily exist, so External code blocks must not use declared variables of internal code blocks.
4. Two code blocks that are not affiliated with each other cannot run at the same time, that is, they cannot exist at the same time, so the variables declared by each have the same name but no conflict.
1.02 Class variables (Class Variables),
whether they are code or variables, only have operations that are run while they are alive. Java can use the keyword "static" to keep variables in existence.

class Information{	
	//int a;
	static int a;
}
public class My_information {

	public static void main(String[] args) {
		//Information x = new Information();
		//x.a = 10;
		//System.out.println("x="+x.a);
		Information.a = 10;
		System.out.println(Information.a);
	}

}
Published 19 original articles · Likes2 · Visits 1103

Guess you like

Origin blog.csdn.net/qq_42692319/article/details/102585933