Things about Java inner classes let you see at a glance

  • 2020-04-01 01:50:23
  • OfStack

In "(link: http://jb51.net/article/36294.htm) some of the things you can learn more about Java inner class, but there are also some of the inner class place worthy of our careful study...

Here are some of the things I've put together for the Java inner class to share...
One: static inner classes can have static members, while non-static inner classes cannot
How do you understand this?
Take a look at the following code:



 package com.b510.test;

 public class Test {
     private int number = 1;

     //Non-static inner classes can have non-static members
     private class InnerTest {
         //Error  Non-static inner classes cannot have static members
         // private static int inNumber = 2;
         private int inNumber = 2;

         public InnerTest() {
             setNumber(2);
             inNumber = inNumber + number;
             System.out.println("innerTest---" + inNumber);
         }
     }

     //Private method of Test
     private void setNumber(int number) {
         this.number = number;
     }

     //The constructor
     public Test() {
         InnerTest in = new InnerTest();
         System.out.println("test");
     }

     public static void main(String[] args) {
         Test test = new Test();
         // innerTest---4
         // test
     }
 }

The first concept is not easy to understand...
Two: non-static members of the static inner class can access the static variables of the external class, but not the non-static variables of the external class
This involves the relationship between static inner and outer classes:


 package com.b510.test;

 public class Test {
     private static int number = 1;
     private String name = "test";

     //Static inner class
     private static class InnerTest {
         //Static inner class  You can have non-static members 
         private int inNumber = 2;

         public InnerTest() {
             //Static inner classes can access static members of external classes
             setNumber(2);
             inNumber = inNumber + number;
             System.out.println("innerTest---" + inNumber);
             //Error static internal class cannot access external class & NBSP; Is a non-static member of
             //System.out.println(name);
         }
     }

     //Static private method of Test
     private static void setNumber(int n) {
         number = n;
     }

     //The constructor
     public Test() {
         InnerTest in = new InnerTest();
         System.out.println("test");
     }

     public static void main(String[] args) {
         Test test = new Test();
         // innerTest---4
         // test
     }
 }

This is actually very easy to understand, I don't know if you seem to understand code 15~23...
Three: non-static members of a non-static inner class can access non-static variables of an external class
This is already mentioned in the first one: 17 lines of one code

1 inNumber = inNumber + number;

Number is a non-static member of an external class, and inNumber can be accessed as a member of a non-static inner class

Is it easy to understand...

To summarize:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/2013050215222630.png ">


Related articles: