Java Technology Topics Overview within the General Chapter

Source: Internet
Author: User

The definition of some classes contains the definition of another class. This internal class is called an inner class (inner class).

For example:

public class OuterClass
{
public class InnerClass
{
//---This is a inner class.
}
}

The Innerclass class is nested in Outerclass and is declared public, and is therefore accessed by objects outside of the Outerclass class.

The Innerclass class is not declared as a static member of the Outerclass class, so no Innerclass object can be generated unless a Outerclass object is generated. However, when you declare a class object that contains nested classes, you do not necessarily generate objects of nested classes, which are primarily determined by the method of constructing the class that contains the nested class.

Example 1:

class OuterClass
{
class InnerClass
{}
}
public class Test
{
public static void main(String[] args)
{
OuterClass out = new OuterClass();
OuterClass.InnerClass in = out.new InnerClass();
}
}

We used outerclass out = new Outerclass (), and the statement generated a Outerclass class object. The Outerclass.innerclass in = Out.new innerclass () is then used, and the statement generates an object of an inner class with an instance of an external class. The two statements in the main () method can also be replaced with the following statement:

Outerclass.innerclass in = new Outerclass (). New Innerclass ();

In summary: In one Class (Test), a non-static inner class (Innerclass) in which another class (Outerclass) is created must have recourse to an instance of this outer class (Outerclass). and the declaration of this inner class object must also be in the form of Outerclass.innerclass.

Example 2:

public class Test
{
class InnerClass
{}
1
public static void main(String[] args)
{
InnerClass in = new Test().new InnerClass();
}
}

The inner class of this class can be instantiated in the main () method in the form of the previous example.

Example 3:

public class Test
{
class InnerClass
{}
InnerClass in = new InnerClass();
public static void main(String[] args)
{}
}

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.