Classification of internal classes and benefits of using internal classes

Source: Internet
Author: User

I. Classification of internal classes

Internal classes are mainly divided into ordinary internal classes, local internal classes, anonymous internal classes, nested internal classes (static internal classes).

Static members cannot be defined in non-static inner classes, and static inner classes cannot access non-static members of external classes.

1 Ordinary inner class (member inner Class)

The difference between an inner class and a normal class: 1 The name of the inner class is nested within the outer class; 2 the outer class will have a method that returns a reference to an inner class.

If you want to create an object of an inner class from anywhere other than a non-static method of an external class, you must obtain a reference to the inner class object as a outerclassname.innerclassname. Building an internal class reference requires a reference to its perimeter class object, and if the compiler does not access the reference, it complains. The fields and methods of the normal inner class can only be placed on the outer level of the class, so the normal inner class cannot have static data and static fields, and should not contain nested classes (static inner classes).

As shown in the following code:

/**
 * Write a class called Outer that contains a class named inner. Add a method to the outer class,
 * It returns an object of inner type, and in main (), creates and initializes a * reference to a Inner object
 .
 * * Public
class Excrise1 {/
       * defines an internal class *
       /Class inner{
              Inner () {System.out.println ()
                     Get a reference to an inner class ")
              ;
       }
       /* An external class defines a method that returns a Inner type object/public
       Inner Getinner () {return
              new Inner ();
       }
       public static void Main (string[] args) {
              Excrise1 e = new Excrise1 ();
              /* in main (), create and initialize a reference to a Inner object/
              Excrise1.inner i = E.getinner ()//Method One: Returns an internal class reference
by calling an external class Excrise1.inner i2 = e.new Inner ()//Method Two: Create a new Inner class object reference using external object reference
}

2 Local Internal classes

Create a complete class within the scope of a method or code, called a local inner class, whose scope is the code block in which it is used, and is the least useful type in the inner class. Local internal classes, like local variables, cannot be decorated with public, protected, private, and static, and can only access local variables defined as final types in the method. It is divided into static local inner class (inside static method or static code block) and non-static local inner class.

3 Anonymous Inner class

Anonymous inner classes are somewhat limited in comparison to regular inheritance, because anonymous inner classes can either extend classes or implement interfaces, but not both. And if the interface is implemented, only one interface can be implemented. Anonymous inner classes do not have inner classes of class names, do not use keyword class, extends, implements, have no constructors, must inherit other classes or implement other interfaces. The advantage of anonymous inner classes is that the code is more concise and compact, but the downside is the readability decline. Commonly used in GUI programming to implement event processing.

4 Nested internal classes

When an inner class is static, it is called a nested class. Then 1 creates an object of a nested class no longer requires an object of its outer class; 2 Cannot access non-static peripheral class objects from within the object of the nested class.

Static internal classes cannot access ordinary member variables of external classes, only static members and static methods in external classes.

Second, the use of internal categories of benefits:

1 internal class can have multiple instances, each instance has its own state information, and its peripheral class object that information is independent of each other;

2 in a single perimeter class, you can have multiple inner classes implement the same interface in different ways, or inherit the same class;

3 The time to create an internal class object does not depend on the creation of the outer class object;

4 The inner class does not have a confusing "is-a" relationship, it is an independent entity.

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.