Java Nested Class

Source: Internet
Author: User

Normal Class (Non-nested Class) can only is public, abstract or final. For nested classes, the static is allowed.

Nested classes (private, public, protected), 2 Categories.

1. Static Nested Class (Very Different from C # Static Class)

Alpha:have access to all the static members of its parent class.

Beta:can instantiate objects by New Parentclass.staticnestclass (). Now allowed to create objects in C #.

Gamma:can has non-static members.

Delta:same as Gamma. Cannot access parentclass.this.

2. Non-static Nested Class (Inner Class)

Alpha:have access to all the members of their parent class.

Beta:can instantiate objects by New ParentClass (). New Innerclass ().

Gamma:cannot has the static members, the except for final static fields.

Delta:variable Shadowing:

ParentClass.this.memberName, Parent class member.

This.membername, ParentClass.InnerClass.this.memberName-Inner class member

======================================

1. Interface can only is defined inside a top-level class, an Interface or in static context because interfaces is inhere ntly Static.

2. Anonymous Classes:

Interface/abstractclass obj = new Interface/abstractclass (Empty for Interface. Initialize for abstract class) {

String/int ...;

public void Implementabstractmethoda () {...}

public void Implementabstractmethodb () {...}

}; <--semicolon in the end

Java Nested Class

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.