Java Internal class Learning (ii)

Source: Internet
Author: User
Tags readable

Nested Classes

The Java programming language allows you to define a class within another class. Such A class is called a nested class and are illustrated here:

class Outerclass {    ...     class NestedClass {        ...    }}
Terminology:Nested classes is divided into the categories:static and non-static. Nested classes that is declared staticis called Static Nested Classes. Non-static nested classes is called Inner Classes.
class Outerclass {    ...     Static class Staticnestedclass {        ...    }     class innerclass {        ...    }}

A nested class is a member of its enclosing class. Non-static Nested classes (inner classes) has access to all members of the enclosing class, even if they is declared P Rivate. The Static nested classes do not has access to other members of the enclosing class. As a member of OuterClass the, a nested class can is declared private , public , protected , or package private. (Recall that outer classes can is only is declared public or package private.)

Understanding: 1. Inner classes can be considered as members of external classes. A non-static inner class can access all members of an external class, including private members. 2. Static inner classes do not have access to members in external classes. 3. As a member of an external class, an inner class can be declared as private,public,protected. Why use Nested Classes?

Compelling reasons for using nested classes include the following:

    • It is a to logically grouping classes that was only used in one place : If A class was use Ful to only one of the other classes, then it's logical to embed it on that class and keep the together. Nesting such "helper classes" makes their package more streamlined.

    • It increases Encapsulation : Consider-top-level classes, A and B, where B needs access to Membe Rs of A that would otherwise is declared  private . By hiding class B within Class A, a ' s members can be declared private and B can access them. In addition, B itself can is hidden from the outside world.

    • It can leads to readable and maintainable code : Nesting Small classes within top-level classes Places the code closer to where it is used.

Understanding: 1. Facilitates the logical aggregation of classes: If a class is used only by another class, we can embed this in it, which is more conducive to the linearization of the package. 2. Added encapsulation: If Class B needs to access the members of Class A, then A's members cannot be declared private. But when B is used as the inner class of a, then B can access the private members of a (in favor of the encapsulation of a). B is also isolated from the outside. 3. Make your code more readable and maintainable: encapsulate "small" classes in the "big" class for easier use

Static Nested Classes

As with class methods and variables, a static nested class was associated with its outer class. And like the static class methods, a static nested class cannot refer directly to instance variables or methods defined in its Enclosing class:it can use them only through an object reference.

Note:A static nested class interacts with the instance members of their outer class (and other classes) just as any other top-l Evel class. In effect, a static nested class was behaviorally a top-level class that have been nested in another top-level class for PAC Kaging convenience.

Static nested classes is accessed using the enclosing class name:

Outerclass.staticnestedclass

For example, to create a object for the static nested class with this syntax:

Outerclass.staticnestedclass nestedobject =     new outerclass.staticnestedclass ();

Understanding: Just like class member variables and methods, inner classes can be seen as part of an external class. Like static class methods, static inner classes cannot access member variables and methods of external classes: they are accessed only through object references. 1. Accessing static internal class methods: (External class name) 2. Create an internal class instance: the external class name. jing

Inner Classes

As with instance methods and variables, a inner class is associated with a instance of its enclosing class and have Direc T access to this object ' s methods and fields. Also, because an inner class was associated with a instance, it cannot define any static members itself.

Objects that is instances of a inner class exist within an instance of the outer class. Consider the following classes:

class Outerclass {    ...     class innerclass {        ...    }}

An instance of can exist-within an instance of and have direct access to the methods and fields of its InnerClass OuterClass en Closing instance.

To instantiate a inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

Outerclass.innerclass InnerObject = Outerobject. New Innerclass ();

There is special kinds of inner classes:local classes and anonymous classes.

Shadowing

If a declaration of a type (such as a member variable or a parameter name) in a particular scope (such as an inner class O R a method definition) have the same name as another declaration in the enclosing scope and then the declaration shadows the declaration of the enclosing scope. You cannot refer to a shadowed declaration by its name alone. The following example, ShadowTest demonstrates this:

 Public classShadowtest { Public intx = 0; classFirstlevel { Public intx = 1; voidMethodinfirstlevel (intx) {System.out.println ("x =" +x); System.out.println ("This.x =" + This. x); System.out.println ("shadowtest.this.x =" + shadowtest. This. x); }    }     Public Static voidMain (String ... args) {Shadowtest St=Newshadowtest (); Shadowtest.firstlevel FL= St.NewFirstlevel (); Fl.methodinfirstlevel (23); }}

The following is the output of this example:

x = This. x = 1shadowtest. this. x = 0

This example defines three variables named x : the member variable ShadowTest of the class, the member variable of the inner Class FirstLevel , and the parameter in the method methodInFirstLevel . The variable defined as a parameter of the method shadows the variable of the inner x methodInFirstLevel class FirstLevel . Consequently, when you use the variable x methodInFirstLevel in the method, it refers to the method parameter. To refer to the member variable of the inner class FirstLevel with the keyword to this represent the enclosing scope:

this. x);

Refer to member variables this enclose larger scopes by the class name to which they belong. For example, the following statement accesses the member variable of the class from the ShadowTest method methodInFirstLevel :

System.out.println ("shadowtest.this.x =" + shadowtest.  this. x);
Serialization

Serialization of inner classes, including local and anonymous classes, is strongly discouraged. When the Java compiler compiles certain constructs, such as inner classes, it createsSynthetic Constructs; These is classes, methods, fields, and all constructs that does not has a corresponding construct in the source code. Synthetic constructs enable Java compilers to implement new Java language features without changes to the JVM. However, synthetic constructs can vary among different Java compiler implementations, which means that.classFiles can vary among different implementations as well. Consequently, compatibility issues if you serialize a inner class and then deserialize it with a different J RE implementation. See the sections implicit and synthetic Parameters in the sections obtaining Names of Method Parameters for more information About the synthetic constructs generated when a inner class is compiled.

Java Internal class Learning (ii)

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.