/** * Copyright (C), 2017,jumore Tec. * Author: hechengwen * Version: * Date: 2018/5/4 9:23 * Description: * Others: */ public class HashMapTest { private static String _name = "xiaohe"; public int _age = 25; public static void main(String[] args) throws Exception { new HashMapTest().display(); } private static void outerClass(){ System.out.println( "I am an external class static method" ); } /** * static inner class */ static class InnerClass1{ public static String name = "xiaosan" ; public void dispaly(){ // The static inner class can directly access the static variables of the outer class, even if it is private, it cannot access non-static member variables and methods System. out.println("inner class1 : " + _name); outerClass(); } } /** * non-static inner class */ class InnerClass2{ // Non-static inner class, cannot define static members public String name = "xiaosi" ; public void display(){ // Non-static inner class can directly access any member of the outer class System.out.println( "inner class2 : " + _name + " , age : " + _age); } } public void display(){ System.out.println( "The outer class can directly access the static inner class variable: " + InnerClass1.name); // The outer class can create objects directly through the new inner class, without relying on the outer class instance new InnerClass1().dispaly(); // Create a non-static inner class instance: outer class. inner class a = new outer class().new inner class(); HashMapTest.InnerClass2 innerClass2 = new HashMapTest(). new InnerClass2 (); innerClass2.display(); } }