Introduction to Java for beginners 200 cases 92 of Java inner class -- member inner class

Introduction to the author

Author name: Ming Shiyin in programming world
Introduction: CSDN blog expert has been engaged in software development for many years and is proficient in Java and JavaScript. Bloggers also learn and grow step by step from scratch, know the importance of learning and accumulation, and like to fight and upgrade with ADC. Welcome to pay attention and look forward to learning, growing and taking off with you!

introduction

Many Java beginners ask me that it's worrying for the novice to turn around and forget the Java knowledge he has studied very hard. How can Xiaobai grow up quickly and become a big cow?
In fact, there is only one skill to become a big God: "learn more and practice more", so brother Ming sorted out typical practice examples. Through practice, you can quickly improve coding skills and proficiency, so that you can never return on the way to become a big man (remember to practice with your own hands)!

Navigation

✪ introduction to Java white 200 case series directory index
◄ previous article    91.Java Interface
► next article to be updated     

summary

In Java, a class can be defined in another class or a method. Such a class is called an inner class. In a broad sense, internal classes generally include these four types: member internal classes, local internal classes, anonymous internal classes and static internal classes.
The shape is as follows:
If class B is defined in class A, class B is an inner class, also known as a nested class. Relatively speaking, class A is an outer class. If there is multi-layer nesting, for example, there is an inner class B in class A and an inner class C in class B, the outermost class is usually called the top-level class (or top-level class).

Member inner class

Its most common inner class, which is defined as being inside another class.

Example 1

An ordinary inner class

package demo.demo92;
//External class
public class Outer {
    class Inner {
        // Inner class
    }
}

The compiled class file, as shown in the figure:

Conclusion:
The inner class is still an independent class. After compilation, the inner class will be compiled into an independent. Class file, but preceded by the class name and $symbol of the outer class

Example 2

The inner member class can unconditionally access all member properties and member methods of the outer class.

package demo.demo92;
//External class
public class Outer {
	private int a=10;
	private static int b=20;
	private void print(){
		System.out.println("External class printing method");
	}
	private static void static_print(){
		System.out.println("External class static Printing method");
	}
	class Inner {
	    // Inner class
		
		private void test(){
			System.out.println(a);//private attribute of external class
			System.out.println(b);//Static properties of external classes
			print();//Methods of external classes
			static_print();//Static methods of external classes
		}
	}  
}

Example 3

When the inner class of a member has a member variable or method with the same name as the outer class, hiding occurs, that is, by default, the members of the inner class of the member are accessed.
Access to external classes in the following format:

External class. this. Member variable
External class. this. Member method

package demo.demo92;
//External class
public class Outer {
	private int a=10;
	private void print(){
		System.out.println("External class printing method");
	}
	class Inner {
	    // Inner class
		private int a=10;
		private void print(){
			System.out.println("External class printing method");
		}
		
		private void test(){
			System.out.println(a);//private attribute of inner class
			print();//Methods of inner classes
			
			System.out.println(Outer.this.a);//private attribute of external class
			Outer.this.print();//Methods of external classes
		}
	}  
}

Example 4

Although the member inner class can unconditionally access the members of the external class, if you want to access the members of the member inner class in the external class, you must first create an object of the member inner class, and then access it through a reference to this object.

package demo.demo92;
//External class
public class Outer {
	private int a=10;
	private void print(){
		//Instantiate inner class
		Inner inner = new Inner();
		//Access through instantiated objects
		System.out.println(inner.b);
		inner.test();
	}
	class Inner {
	    // Inner class
		private int b=20;
		private void test(){
			System.out.println("Inner class test method");
		}
		
	}  
}

Example 5

If you want to create an object of a member's inner class, you must have an object of an outer class.

package demo.demo92;
//External class
public class Outer {

	class Inner {
	    // Inner class
		private int b=20;
		public void test(){
			System.out.println("Inner class test method");
		}
		
	}  
}
package demo.demo92;

public class Test {

	public static void main(String[] args) {
		//Instantiated outer class
		Outer outer = new Outer();
		//Create an internal class instance from an instance of an external class
		Outer.Inner inner = outer.new Inner();
		//An instance of the inner class calls the test method
		inner.test();
	}
}

function:

Internal class test method

Summary

This section summarizes the "Java internal class - member internal class". I hope it can be helpful to you. Please help [like] + [collection] + [punch in the comment area]. If you are interested in learning java with brother Xiaoming, [pay attention to a wave] won't get lost.

Let me know you by punching in the comment area. Mingge will continue to pay attention to your learning progress!

Navigation

✪ introduction to Java white 200 case series directory index
◄ previous article    91.Java Interface
► next article to be updated     

Popular column recommendation

1.Java game series (Tetris, aircraft war, plant war, zombie, etc.)
2.JavaWeb project practice (library management, online examination, dormitory management system, etc.)
3. Wonderful examples of JavaScript (aircraft war, minesweeping, snake eating, verification code, etc.)
4. Introduction to Java Xiaobai 200 cases
5. Learn Java from zero, learn Java with interest, and learn Java from the perspective of King glory

Keywords: Java

Added by john_nyc on Thu, 07 Oct 2021 10:40:08 +0300