Java reference internal class

Java reference internal class

Direct code demonstration

External and internal code public class TestFather  public String test()  return "External class return value"; > public static class TestChild  public String test()  return "Internal static class return value"; > > public class TestChildObj  public String test()  return "Internal non -static class return value"; > > > 
Solution: public static void main(String[] args)   // Method of calling static internal class 1 TestChild testChild = new TestChild(); testChild.test(); // Method of calling static internal class 2 2 TestChild testChild2 = new TestFather.TestChild(); testChild2.test(); // Call the method of non -static internal class TestFather testFather = new TestFather(); TestChildObj testChild3 = testFather.new TestChildObj(); testChild3.test(); > 
result: Internal static class return value Internal static class return value Internal non -static class return value 

Источник

Java Object Oriented Programming Part 4 — internal classes

When the definition of a class appears in the class body of another class, the class is called Inner, and the class in which the Inner class is located is called Outer.

Contents in class: member variable, member method, constructor, static member, constructor and static code block, internal class

2. Meaning of inner class

When the value of a class is only to serve a class separately, the class can be defined as the internal class in the served class, which can hide the implementation details of the class and easily access the private members of the external class without providing public get and set methods.

3. Classification of internal classes

3.1. Member internal class

Directly put the definition of one class in the class body of another class

Access modifier class Class name of external class < Access modifier class The class name of the inner class < Class body of inner class; >>
public class Outer < //Member variable private int outerNum = 1; //Internal and external repeated variables private int commonNum = 2; //Static member variable private static int staticOuterNum = 3; /** * Member method */ public void outerMethod() < System.out.println("I'm an external class outerMethod method"); >/** * Static method */ public static void staticOuterMethod() < System.out.println("I'm an external class staticOuterMethod Static method"); >class Inner < //Member variable private int innerNum = 1; //Internal and external repeated variables private int commonNum = 20; public void innerShow() < //When it conflicts with the external class, it directly refers to the property name, which is the member property of the internal class System.out.println("Internal commonNum:" + commonNum); //Internal classes access external properties System.out.println("outerNum:" + outerNum); //When it overlaps with the external class attribute name, you can use the external class name. this. Attribute name System.out.println("External commonNum:" + Outer.this.commonNum); System.out.println("staticOuterNum:" + staticOuterNum); //Methods to access external classes outerMethod(); staticOuterMethod(); >> public void outershow() < Inner inner = new Inner(); inner.innerShow(); >public static void main(String[] args) < Outer outer = new Outer(); outer.outershow(); >>
public class Test < public static void main(String[] args) < Outer outer = new Outer(); outer.outershow(); System.out.println("====================== code-block">
Access modifier class Class name of external class < Access modifier static class The class name of the inner class < Class body of inner class; >>
public class Outer < //Member variable private int outerNum = 1; //Internal and external repeated variables private int commonNum = 2; //Static member variable private static int staticOuterNum = 3; static < System.out.println("Outer The static block of is executed"); >/** * Member method */ public void outerMethod() < System.out.println("I'm an external class outerMethod method"); >/** * Static method */ public static void staticOuterMethod() < System.out.println("I'm an external class staticOuterMethod Static method"); >static class Inner < static < System.out.println("Outer.Inner The static block of"); >//Member variable private int innerNum = 1; //Internal and external repeated variables private int commonNum = 20; private static int staticInnerVariable = 30; /** * Member method */ public void innerShow() < System.out.println("innerNum:" + innerNum); System.out.println("Internal commonNum:" + commonNum); System.out.println("staticOuterNum:"+staticOuterNum); staticOuterMethod(); >/** * Static method */ public static void innerStaticShow() < //When called, the Outer class will be loaded first staticOuterMethod(); System.out.println("staticOuterNum"+staticOuterNum); >> public static void callInner() < System.out.println("staticInnerNum"+Inner.staticInnerVariable); Inner.innerStaticShow(); >public static void main(String[] args) < callInner(); >>

External use static internal classes

public class Test < public static void main(String[] args) < Outer.callInner(); System.out.println("===================="); Outer.Inner.innerStaticShow(); System.out.println("=================== code-block">
Access modifier class Class name of external class < Access modifier return value type member method name (formal parameter list) < class The class name of the inner class < Class body of inner class; >> >
public class Outer < //Member variable private int outerNum = 1; //Internal and external repeated variables private int commonNum = 2; //Static member variable private static int staticOuterNum = 3; static < System.out.println("Outer The static block of is executed"); >/** * Member method */ public void outerMethod() < boolean flag=false; System.out.println("I'm an external class outerMethod method"); class Inner< //Member variable private int innerNum = 1; //Internal and external repeated variables private int commonNum = 20; /** * Member method */ public void innerShow() < System.out.println("flag:" + flag); System.out.println("innerNum:" + innerNum); System.out.println("Internal commonNum:" + commonNum); System.out.println("External commonNum:" + Outer.this.commonNum); System.out.println("staticOuterNum:"+staticOuterNum); staticOuterMethod(); >> new Inner().innerShow(); > /** * Static method */ public static void staticOuterMethod() < System.out.println("I'm an external class staticOuterMethod Static method"); >public static void main(String[] args) < new Outer().outerMethod(); >>
  • Local internal class, cannot have access modifier before class
  • Local inner classes can only be used inside methods
  • Cannot create static information
  • Local variables and parameters within a method can be accessed directly, but cannot be changed
  • You can access any information of external classes at will
  • Local variables used in methods of anonymous inner classes must be defined as final

3.4. Anonymous inner class

An inner class without a name

Interface/The parent type references the variable name = new Interface/Parent class type() < Method override >;
public class Outer < public static void main(String[] args) < //Interface / parent type reference variable name = new interface / parent type () ; Animal animal=new Animal() < @Override public void speak() < System.out.println("cat "); >>; animal.speak(); > >
  • Anonymous inner classes do not have access modifiers
  • When using anonymous inner classes, the class after new must first exist. Secondly, we need to override one or some methods of the class after new
  • Anonymous inner classes have the same restrictions as local inner classes when accessing method parameters
  • Anonymous inner class has no constructor

Posted by natgopal on Thu, 02 Sep 2021 21:31:30 -0700

Hot Keywords

  • Java - 6961
  • Database - 2683
  • Python - 2616
  • Programming - 2419
  • Attribute - 2418
  • Javascript - 2383
  • Spring - 2111
  • Android - 2077
  • xml - 1972
  • Linux - 1818
  • JSON - 1764
  • network - 1748
  • github - 1720
  • less - 1676
  • MySQL - 1538
  • SQL - 1332
  • PHP - 1318
  • encoding - 1179
  • Mobile - 1029
  • Apache - 925

Источник

Internal classes in Java

In short, the biggest advantage of inner classes is that they can solve the problem of multiple inheritance. That is, each internal class can inherit the implementation of an interface independently without being restricted by the implementation of external class inheritance.

1. Internal foundation

The internal category is generally divided into four categories

  • Member inner class
  • Static inner class
  • Local inner class
  • Anonymous Inner Class

1.1 member internal class

Member inner class is the most common "defined in a class" class.

class Circle < private double radius = 0; public Circle(double radius)< this.radius = radius; >//Draw is a member inner class class Draw < public void drawSahpe() < //The inner class of a member can access all member properties and member methods of the outer class, whether private or static System.out.println(radius); >> >

Considerations when using member inner classes

  1. There cannot be static member properties and methods in the internal class of a member. However, if a variable is of basic data type or String type and is decorated with static final, the compilation can pass
    • The reason why there can be no static member properties and methods is that the internal class of non static members depends on the existence of external classes
    • There can be basic data type or String type member variables of static final because these variables are placed in the constant pool and do not involve class loading
  2. The inner class of a member can access all member properties and member methods of the outer class, whether private or static
  3. The external class can also access all member properties and member methods of the internal class of the member, but first create an object of the internal class of the member, and then access the members of the internal class through this object reference
  4. When there is a conflict between the member variables or methods of the inner class and the outer class, the members of the outer class will be hidden. If you want to access the members of the external class in the internal class at this time, you can access them in the following ways
    • External class this. Member variable
    • External class this. Member method
  5. Internal classes can modify access rights
    • private, which can only be accessed in external classes
    • protected, which can only be accessed under the same package or in classes that inherit external classes
    • public, which can be accessed anywhere
    • Access under the same package by default

1.2 static internal class

Static inner classes are similar to member inner classes, except for the static modifier.

Considerations when using static inner classes

  1. Static member variables and methods are allowed in static inner classes, while static member variables and methods are not allowed in non static inner classes, except for the basic data type or String of static final
  2. Static classes cannot use member variables and methods that are not static in any external class. Because Java can create static internal class objects without creating external class objects, while the non static member variables and methods of external classes exist attached to external class objects, which are contradictory to each other
  3. The biggest difference between static internal classes and non static internal classes is that after compilation, the non static internal class always holds a reference to the external class object that created it, while the static internal class does not and does not need this reference. Therefore, the creation of static internal class objects does not need to be attached to external classes

1.3 local internal class

A local internal class is a class defined in a method or scope. The access permission of a local internal class is only within the method or scope.

Considerations when using local inner classes

  1. The status of the local inner class is similar to the local variable of the external method, so as long as this method is out, the local inner class cannot be accessed
  2. Local internal classes cannot use access modifiers, including private, protected, public and static. The fundamental reason lies in Article 1 - access cannot be accessed without methods, so the access modifier is meaningless
  3. You can access the member variables of the external class. If the local internal class is defined in the static method, you can only access the static variables of the external class
  4. You can use final or abstract modifiers

1.4 anonymous inner class

Anonymous inner class is a class without a name. The main advantage is that it is easy to write, but there is a premise that the inner class must inherit or implement an external class or interface

public class OuterClass < public InnerClass getInnerClass(final int num,String str2)< return new InnerClass()< int number = num + 3; public int getNumber()< return number; >>; > public static void main(String[] args) < OuterClass out = new OuterClass(); InnerClass inner = out.getInnerClass(2, "chenssy"); System.out.println(inner.getNumber()); >> //There should be a declaration of the interface here interface InnerClass

Considerations when using anonymous inner classes

  1. Anonymous inner classes can't use modifiers. There's no place to write them
  2. When new an anonymous inner class, there must be a declaration of this class or interface
  3. Anonymous inner class has no constructor
  4. When the formal parameter of the method needs to be used in the anonymous inner class, the formal parameter must be final. Here, final means that the value of the variable used cannot be modified in the anonymous inner class. Although it does not need to be declared final in Java 8, it still cannot be modified in the anonymous inner class. Fundamentally, Java's implementation of anonymous internal class variable passing is based on constructor parameter passing. If the value is allowed to be modified in the anonymous internal class, what is actually modified is only a copy of the variable. In fact, it has no impact on the variable of the external class, which will have an impact on the program - modified, but not completely modified

2. Advanced internal class

2.1 external class references held by internal classes

Two bytecode files are generated after compilation

  • Demo.class
  • Demo$DemoRunnable.class
    Where demo $runnable Class file is the bytecode file compiled by the internal class. The file compiled by the internal class is named external class name $internal class name. At this time, the decompiled code is as follows
package inner public class Demo$Runnable implements Runnable() < public Demo$Runnable (Demo var1)) < this.this$0 = var1; >public void run() < >>

In other words, the reference of the external class object is passed into the internal class object as the parameter of the constructor of the internal class.

2.2 why can only access final variables in anonymous inner classes

We view the bytecode of decompiled internal class variables in three cases

  • Non final local variable
  • final local variable
  • External class member variable

Non final local variable

At this time, the byte code of the decompiled internal class variable is

package inner; public class Demo$Runnable implements Runnable() < public Demo$Runnable (Demo var1, int var2)) < this.this$0 = var1; this.this$age = var2; >public void run() < int var1 = this.val$age + 1; System.out.println(var1); >>

It can be found that non final variables are passed in as formal parameters

final local variable

At this time, the byte code of the decompiled internal class variable is

package inner; public class Demo$Runnable implements Runnable() < public Demo$Runnable (Demo var1)) < this.this$0 = var1; >public void run() < byte var1 = 11; System.out.println(var1); >>

It can be found that the final variable is directly optimized into a byte value during compilation. When this variable cannot be determined during compilation, it will be passed in as a formal parameter like the non final variable

External class variable

At this time, the byte code of the decompiled internal class variable is

package Demo; public class Demo$1 implements Runnable() < public Demo$1(Demo var1)) < this.this$0 = var1; >public void run() < int var1 = this.this$0.age + 1; System.out.println(var1); this.this$0.age = 20; >>

It can be found that at this time, the external class variables are operated through the reference of the external class.

Posted by srsimmons on Tue, 15 Mar 2022 03:53:33 +1030

  • Java - 5996
  • Python - 2604
  • Algorithm - 1737
  • Javascript - 1708
  • Back-end - 1577
  • Front-end - 1561
  • Linux - 1389
  • C++ - 1378
  • data structure - 1135
  • Database - 953
  • Spring - 922
  • C - 836
  • MySQL - 835
  • Android - 683
  • Spring Boot - 660
  • Vue.js - 549
  • Design Pattern - 543
  • Operation & Maintenance - 509
  • Deep Learning - 487
  • Interview - 459

Источник

Читайте также:  Python str methods documentation
Оцените статью