Nested-class and inner-class in Java

Source: Internet
Author: User

Refer to this articleArticle:

Http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Differences between Nested-class and inner-class:

Nested classes are divided into two categories: static and non-static. Nested classes that are declared
StaticAre simply calledStatic Nested classes. Non-static Nested classes are called
Inner classes.

In the following example, staticnestedclass is a nested class and innerclass is an internal class.

 
Class outerclass {... static class staticnestedclass {...} class innerclass {...}}

Both Nested-class and inner-class are member variables of the outer class. Only one is static and the other is non-static.

Therefore, both Nested-class and inner-class can be declared as private, public, protested, or package private (recall here, the general class can only be public or package private when declared ).

But there are differences between static and non-static: Nested-class is related to the class, and inner-class is related to the instance. Therefore, you can directly access the nested-class without instantiating the object, but you must instantiate the class to access the inner-class. Nested-class cannot access other member variables or methods of the outer class, while inner-class can.

Here is another interesting thing: Because inner-class is a non-static member variable, no static variables or methods can be defined in inner-class.

Related Article

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.