JAVA class summary, java class Summary

Source: Internet
Author: User

JAVA class summary, java class Summary
JAVA class Summary

I recently read some content related to java internal classes and made some summary. Released in sync with my blog zhiheng. me. Title: JAVA class summary.

Top-level and Nested classes

A class defined within a class (or interface, the same below) is called a nested class. The corresponding class is called the peripheral class of the class (enclosing class) or package class.

A non-nested class is called a top-level class. A java file can contain several top-level classes (including abstract classes and interfaces), but only one class can be modified by the public, and the class must match. the java file has the same name.

The top-level class access modifier can only be public and package access permissions (default permission, no modifier ).

A nested class can be seen as a member of a peripheral class, so its modifier can be public, protected, package access permission, and private.

There is no hierarchy restriction on Nested classes. You can define classes in the nested classes to become Nested classes in the nested classes.

There are two types of nesting classes. One is static (modified with the static keyword), and the other is non-static, it is called an inner class ).

Note: in Think in Java, the author defines an internal class as "defining a class within the definition of another class, then this class is an internal class". Therefore, he regards static Nested classes as one of internal classes. This document uses the definition in the official java documentation.

An internal class is generally directly defined in an external class. Like a member of this class, we call this internal class A member internal class ). That is, internal classes not defined in constructors, methods, and statement blocks are member Internal classes.

In addition to member Internal classes, there are two special internal classes: local class and anonymous internal class ).

Name of nested bytecode files

After the nested class is compiled, an independent bytecode file (. class) is automatically generated. Its naming format is as follows:

External class name + \ $ + [sequence of classes with the same name of the Class] + [internal class name]

The following code (File Name: Outer. java) contains Static class Static, Inner class of the member, Local class, Anonymous class implementing the Anonymous interface, and Anonymous interface defined in the source file.

 

package thinkinjava; public class Outer {     public static class Static{}     public class Inner {}     {        class Local{};    }     Anonymous anonymous = new Anonymous(){};} interface Anonymous {}

After compilation, the following six. class bytecode files are formed. Both the top-level class Outer and Anonymous are compiled into class files with the same name. Static Nested classes Static and member internal class Inner are compiled into Outer \ $ Static. class and Outer \ $ Inner. class, because the Member class cannot have the same name, there is no class order of the same name. The locally compiled file name of the Local class is Outer \ $ 1Local. class. Because there is only one Local class named Local in the Outer class, the order is 1. The anonymous class has no name, so the compiled file name is Outer \ $1. class. 1 indicates that this class is the first Anonymous class in the Outer class.

Anonymous.classOuter$1.classOuter$1Local.classOuter$Inner.classOuter$Static.classOuter.class
Static nesting class
public class OuterClass{    public static class NestClass{}}

Static Nested classes are static, so the relationship between them and external classes is more like the relationship between classes and packages. When referencing and using other classes, you need to add external class restrictions:OuterClass.NestClass.

Internal class

The internal class is non-static, so the internal class is associated with the instance of the external class. When instantiating an internal class, you must first instantiate the external class, and then create an internal class instance through the instance of the external class:

OuterClass outObject = new OuterClass();OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Shadowing)

Defines fields or parameters in an internal class or member method. If the same name is defined as a member variable in an external scope, the external definition will be masked, in this case, you cannot access external members only by name within the internal scope. The following is an example taken from Java Tutorial:

Public class ShadowTest {public int x = 0; class FirstLevel {public int x = 1; void methodInFirstLevel (int x) {System. out. println ("x =" + x); System. out. println ("this. x = "+ this. x); System. out. println ("ShadowTest. this. x = "+ ShadowTest. this. x) ;}} public static void main (String... args) {ShadowTest st = new ShadowTest (); ShadowTest. firstLevel fl = st. new FirstLevel (); fl. methodInFirstLevel (23) ;}/// the output is as follows // x = 23 // this. x = 1 // ShadowTest. this. x = 0
Local internal class

A local class is a class defined in a block. That is, the class defined in the constructor, method, loop body, and branch structure (if clause.

The local class belongs to the block scope. Therefore, you can access local variables (including parameters), but you can only access local variables modified with final.

After Java SE 8, local classes can accessEffectively finalLocal variables and non-final parameters,Effectively finalThe variable is not final modified, but has never changed the value after initialization."A variable or parameter whose value is never changed after it is initialized is already tively final".

 

Anonymous class

An anonymous class, as its name implies, is a class without a name. Without a name, it cannot be referenced or instantiated elsewhere. Of course, there is no constructor. The anonymous class is instantiated at the same time as it is defined (the Anonymous class is instantiated only once ).

The definition of an anonymous class is more like an expression, that is, the definition of a class appears in an expression. In syntax form, the definition of an anonymous class is like calling a constructor. The following are examples of several anonymous classes:

Public class Test {InterfaceA a = new InterfaceA () {}; // member Anonymous class public static void main (String [] args) {InterfaceA a = new InterfaceA (){}; // partial Anonymous class // the above two methods are called interface-Based Anonymous classes by implementing interfaces. You can also implement Test test = new Test () {} through inheritance classes (){}; // The inherited Anonymous class // It can also be located on the parameter new Thread (new Runnable () {@ Override public void run (){}}). start (); // a type of partial anonymity} private interface InterfaceA {}}

The anonymous class cannot use any keyword or access control operator. The Anonymous class is the same as the Department class access rule, but the internal class explicitly defines a class and then creates this local class instance in the new way, the anonymous class directly creates a new class instance.

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.