Java internal class, Java

Source: Internet
Author: User

Java internal class, Java
Java internal class

You can put the definition of a class inside the definition of another class, which is the internal class.

Internal classes are very useful because they allow you to organize logical-related classes and control the visibility of internal classes. Internal classes and combinations are completely different concepts.

import java.util.*;public class Parcel2{class Contents{private int i=11;public int value(){return i;}}class Destination{private String label;Destination(String whereTo){label=whereTo;}String readLabel(){return label;}}public Destination to(String dest){return new Destination(dest);}public Contents contents(){return new Contents();}public void ship(String dest){Contents c=contents();Destination d=to(dest);System.out.println(d.readLabel());}public static void main(String[] args){Parcel2 p=new Parcel2();p.ship("HongKong");Parcel2 q=new Parcel2();Parcel2.Contents c=q.contents();Parcel2.Destination d=q.to("Beijing");}}

When an internal class object is generated, this object has a connection with the peripheral object that creates it, so it can access all the members of its peripheral object, no special conditions are required. In addition, the internal class also has access to all elements of the external class.

import java.util.*;interface Selector{boolean end();Object current();void next();}public class Sequence{private Object[] items;private int next=0;public Sequence(int size){items=new Object[size];}public void add(Object x){if(next<items.length){items[next++]=x;}}private class SequenceSelector implements Selector{private int i=0;public boolean end(){return i==items.length;}public Object current(){return items[i];}public void next(){if(i<items.length)i++;}}public Selector selector(){return new SequenceSelector();}public static void main(String[] args){Sequence sequence=new Sequence(10);for(int i=0;i<10;i++)sequence.add(Integer.toString(i));Selector selector=sequence.selector();while(!selector.end()){System.out.print(selector.current()+" ");selector.next();}System.out.println();}}

When we first saw SequenceSelector, we thought it was just an internal class. But observe it carefully. Note that objects is used in methods such as end (), current (), and next (), which is a reference and is not part of SequenceSelector, it is a private field in the peripheral class. However, internal classes can access the methods and fields of external classes, just as they own them, which makes it very convenient.

Therefore, the internal class automatically has access to all the members of its peripheral class. How is this done? When an internal class object is created for a peripheral class object, the internal class object will secretly capture a reference pointing to that peripheral class object. Then, when you access the members of this peripheral class, you use the reference to select the members of the peripheral class. Therefore, an internal class object can be created only when it is associated with its peripheral Class Object (when the internal class is not static ).


Nested class

If you do not need to associate an internal class object with a peripheral class object, you can declare the internal class as static, which is usually called a nested class. Nested classes mean:

1) To create nested class objects, you do not need to create peripheral class objects;

2) non-static peripheral class objects cannot be accessed from nested class objects;

There is another difference between a nested class and a common internal class: the fields and methods of a common internal class can only be placed at the external level of the class. Therefore, a common internal class cannot have static data or static fields, it cannot contain Nested classes, but Nested classes can contain all these things.


Why Internal classes?

Each internal class can inherit the implementation of an interface independently, so no matter whether the peripheral class has inherited the implementation of an interface, it has no impact on the internal class.

Internal classes make the multi-inheritance solution complete. The interface solves some problems, while the internal class effectively implements "Multi-inheritance", that is, the internal class allows inheritance of multiple non-interface types.


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.