Directly cut to the subject, give you the most core, super detailed and in-depth understanding of Java internal classes

What is an inner class:

In addition to attributes and methods, a class can also have other classes, and internal classes can also be in methods or code blocks.  (Code examples will follow)

It can be simply understood as defining another class inside the curly braces {}, then this class is called: inner class.  In theory, the inner class can be anywhere in the class, so the code block or ordinary method can also be used.

The role of the inner class:

1. Enhance encapsulation, put the inner class in the outer class, and not allow other classes to access the inner class.

2. Increase the maintainability of the code, does not involve other classes.

3. The inner class can directly access the members of the outer class.

There are four internal categories:

  • Instance inner class: a class defined directly in the class, without any modifier in front of the class.
  • Static inner class: add a static before the inner class.
  • Local inner class: the inner class defined in the method.
  • Anonymous inner class: belongs to a special case of local.

Instance inner class

//外部类
//外部类
 class Outer {
	 String str = "一颗剽悍的种子";
	//内部类
	class Inner{
		void fun() {
			System.out.println(str);
		}
	}
}
public class Test2{
	public static void main(String[] args) {
		Outer out = new Outer();
		//实例内部类
		out.new Inner().fun();
	}
}

operation result:

Directly cut to the subject, give you the most core, super detailed and in-depth understanding of Java internal classes

 

The above code example has a detail: it is the function of the inner class we mentioned, the  inner class can access the members of the outer class.

The format of instantiating inner classes:

new outer class().new inner class();

The code itself is very simple, as long as you understand the syntax of the class and the way the object is instantiated, it is interesting to understand its bottom layer. What happens when the inner class is created? When creating the inner class, there will be a reference to the outer class.

We can use the compiled class file to see through our decompiler tool.

class Outer$Inner
{
	final Outer this$0;
	void fun()
	{
		System.out.println(str);
	}
	Outer$Inner()
	{
		this$0 = Outer.this;
		super();
	}
}

As you can see from the code above, in addition to its own references in the inner class, there are also references to the outer class. In the heap, it is equivalent to having one address for the external class and two addresses for the internal class, one for its own internal class, and the other for the address of the external class.

Static inner class

Those who have come into contact with the knowledge point of Java static should all know that when it comes to static, it is nothing more than an understanding and application of static keywords, so you may understand better.

To put it more directly, adding a static before the inner class is a static inner class.

//外部类
 class Outer {
	static String str = "一颗剽悍的种子";
	//静态内部类
	static class Inner{
		void fun() {
			System.out.println(str);
		}
	}
}
public class Test2{
	public static void main(String[] args) {
				new Outer.Inner().fun();;
	}
}

operation result

Directly cut to the subject, give you the most core, super detailed and in-depth understanding of Java internal classes

 

Note: You can see that the above members are also declared static with static, because the declaration of static cannot directly call non-static variables or methods.

why?

We all know that static is the first to execute, and it will be automatically called when the bytecode is loaded, and before the main method main, it is earlier than the construction method. At this time, the non-static properties and methods have not yet been initialized.

There are two differences from the above non-static inner class definition:

1. The creation format of static internal classes does not need to be instantiated, the specific format is as follows:

new outer class(). inner class();

2. There is no external class reference in the static inner class.

why?

Because the static inner class belongs to the outer class, it can be seen from the above that it does not use new but uses the method of class.

Or check the class file through our decompilation, there is code and the truth.

static class Outer$Inner
{
	void fun()
	{
		System.out.println(Outer.str);
	}
	Outer$Inner()
	{
	}
}

Local inner class

At the beginning, what is the inner class, it is said that the inner class can be defined not only in the outer class, but also in the method, and the class defined in the method is called the local inner class

//外部类
class Outer {
	void fun_1() {
	String  name = "一颗剽悍的种子";
		 class Inner{
			void fun_2() {
				System.out.print(name);
			}
		}
		 Inner in = new Inner();
		 in.fun_2();
	}
}
public class Test2{
	public static void main(String[] args) {
		new Outer().fun_1();
	}
}

operation result

Directly cut to the subject, give you the most core, super detailed and in-depth understanding of Java internal classes

 

What are the limitations of local inner classes?

1. Local inner classes can only be used in the definition method. You can see the code example. Create an inner class in the method and call the method in the inner class. Subsequent instantiation only needs to call the fun_1 method in the outer class.

2. Can not use public, protected, private decoration.  This one corresponds to the first one. Think about it, you have already created it in the method and only used it in the method. Why do you still use these permissions? ? ?

3. Static variables cannot be used in local classes. As mentioned in the static inner class above, static belongs to the outer class, so the definition of static variables in the inner class in the method will report an error.

In addition to the limitations of local inner classes, you can also see that in code examples, inner classes can directly use variables in methods.

(Before jdk1.7, if internal classes want to access the variables in the method, they need to add final. We know that final is used to declare constants, and fianl needs to set the value and cannot modify it.)

No need after jdk1.8 version, just my jdk1.8 does not need to add final, but in real situations, through the decompiler tool to see the class file will find that it has been automatically added for us during compilation.

(You will find that as the development of the language makes it easier for you to develop, you are also farther away from the essence)

class Outer$1Inner
{
	final Outer this$0;
	private final String val$name;
	void fun_2()
	{
		System.out.print(val$name);
	}
	Outer$1Inner()
	{
		this$0 = final_outer;
		val$name = String.this;
		super();
	}
}

At this point, some friends may ask, why the internal declaration variable needs to be finalized, so I won't expand it here, because it involves the stack relationship between final in the method area and the code after the code is run, and I will write another article next time.

Anonymous inner class

According to the literal meaning, there is no named inner class, then anonymous inner class has no constructor, because we all know that the constructor of the class needs to be named the same as the class.

new parent class or interface

The above focuses on four internal classes, instance internal classes, static internal classes, partial internal classes, and anonymous internal classes. Because the application of anonymous internal classes is relatively small, there will be no code examples simply to talk about it. When it is useful and good For example, you can write a separate article.

Author: CSDN bloggers "decisions based on a seed."

Original link: https://blog.csdn.net/A_hxy/article/details/106985170

Guess you like

Origin blog.csdn.net/yunduo1/article/details/109116592