Java static inner class

Source: Internet
Author: User

Static Inner class features:
1. A member inner class that uses static adornments is a static inner class
2. Static internal classes and external classes have no relationship, but are only affected when generating class names and class definitions. Static inner classes can be seen as classes that are lateral to the outer class. The use method is exactly the same as the class on the outer class.
3. Create an instance of a static inner class using the external class name. Internal class Name Instance name = new External class name. Internal class name (parameter)
Static internal classes have the following limitations:
1. Static inner classes cannot have duplicate names with external classes
2. Static inner classes cannot access non-static properties and methods of external classes. External classes do not have access to the non-static properties and methods of the inner class.

public class Staticinnerclass {public static void main (string[] args) {//TODO auto-generated method Stubouter1.inner1 Inn ER = new Outer1.inner1 ();//construct Static inner class object Inner.inner (); System.out.println ("*******************"); Outer1 outer = new Outer1 (); Outer.outer ();}} Class Outer1{private String name = "Zhang San";p rivate int num1 = 1;private static int num3 = 100;public void outer () {System.out . println (name+ "" +num1); Inner1 inner1 = new Inner1 (); Inner1.inner (); System.out.println (inner1.name);//Can call static inner class, need to create object System.out.println (INNER1.NUM2); System.out.println (INNER1.NUM3);}  public static class Inner1{private String name = "John Doe";p rivate static int num2 = 2;private static int num3 = 99;public void Inner () {System.out.println (name+ "" +num2);//system.out.println (outer1.this.name+ "" +outer1.this.num1);// Static inner classes cannot access non-static members of external classes//outer ();//outer1.this.outer (); System.out.println (num3);//system.out.println (OUTER1.THIS.NUM3); System.out.println (OUTER1.NUM3);//static inner class calls the external class member without this. The way, directly removed}}}

  

Java static inner class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.