This is the 18th day of my participation in the August Challenge

In Java, you can define a class within another class, called a nested class. They enable you to logically group classes that are only used in one place, increasing the use of encapsulation and creating more readable and maintainable code.

  • The scope of a nested class is limited by the scope of its enclosing class.

  • Nested classes can access members of nested classes, including private members. However, the reverse is not true, that is, closed classes cannot access members of nested classes.

  • A nested class is also a member of its enclosing class.

  • As members of its enclosing class, nested classes can be declared private, public, protected, or Package Private (the default).

  • Nested classes fall into two categories:

    1. Statically nested classes:Is declared as a nested classThe staticThis is called a statically nested class.
    2. Inner classes: Inner classes are non-statically nested classes.

Grammar:

class OuterClass
{...class NestedClass
    {... }}Copy the code

Statically nested classes

In the case of ordinary or regular inner classes, there can be no inner class object without an outer class object. That is, an object of an inner class is always closely related to an object of an outer class. But in the case of statically nested classes, there may be statically nested class objects if there are no external class objects. That is, objects of statically nested classes are not strongly associated with external class objects.

Like class methods and variables, statically nested classes are associated with their external classes. Like static class methods, a statically nested class cannot directly reference instance variables or methods defined in its enclosing class: it can only use them through object references.

Use closed class names to access them.

OuterClass.StaticNestedClass
Copy the code

For example, to create an object for a statically nested class, use the following syntax:

OuterClass.StaticNestedClass nestedObject = 
     new OuterClass.StaticNestedClass ();
Copy the code
// demonstrates Java programs that access statically nested classes

class OuterClass
{
	// Static objects
	static int outer_x = 10;
	
	// Instance (non-static) object
	int outer_y = 20;
	
	// Private objects
	private static int outer_private = 30;
	
	// statically nested classes
	static class StaticNestedClass
	{
		void display(a)
		{
			// Static members of the external class can be accessed
			System.out.println("outer_x = " + outer_x);
			
			// Display private static members of external classes can be accessed
			System.out.println("outer_private = " + outer_private);
			
			// The following statement gives a compilation error
			// Because statically nested classes cannot directly access non-static members
			// System.out.println("outer_y = " + outer_y);}}}// Driver class
public class StaticNestedClassDemo
{
	public static void main(String[] args)
	{
		// Access statically nested classes
		OuterClass.StaticNestedClass nestedObject = newOuterClass.StaticNestedClass(); nestedObject.display(); }}Copy the code

Output:

outer_x = 10
outer_private = 30
Copy the code

The inner class

To instantiate an inner class, you must first instantiate the outer class. Then, create an internal object within an external object using the following syntax:

OuterClass.InnerClass innerObject = externalObject.new InnerClass(a);
Copy the code

There are two special inner classes:

  1. Local inner class
  2. Anonymous inner class
// Used to demonstrate Java programs that access inner classes

class OuterClass
{
	// Static objects
	static int outer_x = 10;
	
	// Instance (non-static) object
	int outer_y = 20;
	
	// Private objects
	private int outer_private = 30;
	
	/ / inner classes
	class InnerClass
	{
		void display(a)
		{
			// Static objects that can access external classes
			System.out.println("outer_x = " + outer_x);
			
			// You can also access non-static objects of external classes
			System.out.println("outer_y = " + outer_y);
			
			// Private objects of external classes can also be accessed
			System.out.println("outer_private = "+ outer_private); }}}// Driver class
public class InnerClassDemo
{
	public static void main(String[] args)
	{
		// Access the inner class
		OuterClass outerObject = new OuterClass();
		OuterClass.InnerClass innerObject = outerObject.new InnerClass(a); innerObject.display(); }}Copy the code

Output:

outer_x = 10
outer_y = 20
outer_private = 30
Copy the code

Comparison of normal or regular classes to statically nested classes

Serial number Ordinary inner class Statically nested classes
1. Without an external class object, there can be no inner class object. That is, an inner class object is always associated with an outer class object. If there are no external class objects, there may be statically nested class objects. That is, statically nested class objects are not associated with external class objects.
2. Static members cannot be declared in normal/regular inner classes. In a statically nested class, static members can be declared.
3. Because you cannot declare the main() method, you cannot call a regular inner class directly from the command prompt. Because you can declare the main() method, you can invoke statically nested classes directly from the command prompt.
4. Both static and non-static members of an external class can be accessed directly. Only static members of an external class can be accessed directly.

That’s all for this article

I’ve been writing tech blogs for a long time and this is one of my tech posts/tutorials. Hope you like it! Here is a summary of all my original and works of source code: GitHub, and this is my recently just built blog: Haiyong. Site, there is no content, put some HTML small games and small tools, interested can try, source code can be their own F12 copy, or directly find me to.

If you really learn something new from this article, like it, bookmark it and share it with your friends. 🤗 and finally, don’t forget ❤ or 📑.