Overview of internal classes

Source: Internet
Author: User

Internal class

There are two types of internal classes: Member classes (like attributes of external classes) and regional classes (like variables in methods in external classes ).

I. Membership

The internal classes can be divided into: class level (internal class of static Declaration) and object level

InnerClasses. java

Package innerClass;

Public class InnerClasses {

Private static String staticAttribute = "Outter class static attribute ";

Private String instantAttribute = "Outter class instant ";

// Object-level method

Public void instantMethod (){

System. out. println ("Outter class instant method ");

}

// Class-level method

Public static void staticMethod (){

System. out. println ("Outter class static method ");

}

// Category-level internal class

/**

* The methods in this internal class must have class-level attributes and methods of the external class. Otherwise, they are good.

* For example, you can only access the staticAttribute and staticMethod () Methods of external classes.

**/

Public static class StaticInnerClass {

Public StaticInnerClass (){

System. out. println ("static Inner class ");

}

Public void access (){

System. out. println (staticAttribute );

StaticMethod ();

}

}

// Object-level internal class

/**

* This internal class can access all the methods and attributes in the external class.

**/

Public class InstantInnerClass {

Public InstantInnerClass (){

System. out. println ("Instant Inner Class ");

}

Public void access (){

System. out. println (instantAttribute );

StaticMethod ();

}

}

}

 

Internal class access method:

1. Class-level internal classes access internal classes through the class names of external classes

2. Object-level internal classes must be accessed through external class objects

Package innerClass;

Public class TestClass {

Public void callStaticInnerClass (){

// Use the Class Name of the external class for access

InnerClasses. StaticInnerClass sic

= New InnerClasses. StaticInnerClass ();

Sic. access ();

}

Public void callInstantInnerClass (){

// The internal class at the object level can be accessed only by the object of the external class.

// Declare the cashing ic of an external class

InnerClasses ic = new InnerClasses ();

// Internal class at the object access level

InnerClasses. InstantInnerClass iic;

Iic = ic. new InstantInnerClass ();

}

}

 

Abstract internal class

The Static internal abstract class can be accessed by the class name of the external class and inherited by other classes.

An internal class abstract class at the member level, which can only be inherited by other internal classes in this external class

2. Regional

The internal classes can be divided into variable-level and anonymous-level

The internal class at the variable level is the class declared in the method. This internal class is not applicable.

Anonymous internal class. That is, internal classes without class names.

Package innerClass;

Import java. awt. Frame;

Import java. awt. event. WindowAdapter;

Import java. awt. event. javaswevent;

Public class AnonymousInnerClass {

Public static void main (String [] args ){

Frame f = new Frame ("Anonymous Inner Class Example ");

// The parameter of this method is an anonymous internal class application

F. addWindowListener (new WindowAdapter (){

Public void windowClosing (WindowEvent e ){

System. exit (0 );

}

});

F. setSize (200,200 );

F. setVisible (true );

}

}

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.