Summary of internal class learning in Java

Source: Internet
Author: User

These days reading, began to see the Java inside the class, began to think back to see, you can read, the results see the time to monitor there is also no read.

In other words, I write Java code has a problem, it may be written in C + + and C written more, is like to each of the classes are written in a separate file.

There is no problem with nesting or a file that has several classes written.

Then we will summarize the meanings of the inner classes and use them.

Here are some good blogs to share:


Http://www.cnblogs.com/nerxious/archive/2013/01/24/2875649.html

http://android.blog.51cto.com/268543/384844/

Commonality of inner classes


(1), the inner class is still a separate class, after compiling the inner class is compiled into a separate. class file, but preceded by the class name and the $ symbol of the outer class. (2), internal classes can not be accessed in an ordinary way. An inner class is a member of an external class, so the inner class is free to access the member variables of the outer class, whether private or not. (3), the inner class is declared static, you cannot casually access the member variables of the outer class, at which point the inner class can only access static member variables of the outer class.



//======================================================================================//

Speaking of a question here, what is the introduction of internal classes?


Typically, an inner class inherits from a class or implements an interface, and the inner class's code operation creates an object of its outer class. So you can think that the inner class provides some kind of entryThe window of its outer class. The most appealing reasons for using inner classes are: each inner class can inherit itself from one (interface) implementation independently, so no matter whether the perimeter class has inherited an implementation of (an interface), there is no effect on the inner class. If without the ability of an inner class to inherit multiple concrete or abstract classes, some design and programming problems can be difficult to solve. From this point of view, internal classes make multiple inheritance solutionsThe solution becomes complete. The interface solves some of the problems, while the inner class effectively implements "multiple inheritance."


//======================================================================================//


The inner classes are divided into the following types:

1, members of the internal class;

2, the method internal class;

3, anonymous internal class;

4, Static nesting class;

1. member Inner class Example
Class Outer {class inner{}} compiling the code above produces two files: Outer.class and Outer$inner.class.
2. Method Inner classPut the class in the method class Outer {Public void DoSomething () { class inner{Public void Seeouter () { } } } } (1), the method inner class can only be instantiated within a method that defines the inner class, and it cannot be instantiated outside this method. (2), the method inner class object cannot use the non-final local variable of the method in which the inner class resides. because the local variable of the method is on the stack, it only exists within the lifetime of the method. When a method ends, its stack structure is deleted, and local variables become history. But after the method finishes, the inner class object created inside the method may still exist in the heap! For example, if a reference to it is passed to some other code and stored within a member variable. Because there is no guarantee that the lifetime of local variables is as long as the inner class objects of the method, they cannot be used by inner class objects. The following is a complete example: class Outer {Public void DoSomething () { final int a =10; class inner{Public void Seeouter () { System.out.println (a); } } Inner in = new Inner (); In.seeouter (); }Public static void Main (string[] args) { Outer out = new Outer (); out.dosomething (); } }

3. Anonymous inner class as the name implies, there are no names inside the class. On the surface they seem to have names, which are not their names. When an anonymous inner class is used in a program, an object of that class is often created directly where the anonymous inner class is defined. The declaration format of an anonymous inner class is as follows: New ParentName () { ...//definition of inner class }[1] an anonymous inner class is an inner class that has no name. Under what circumstances do I need to use anonymous internal classes? Using an anonymous inner class is appropriate if some of the following conditions are true: • Only one instance of the class is used. • The class is used immediately after the definition. • The class is very small (sun recommended is below 4 lines of code) • Naming a class does not make your code easier to understand. here are a few principles to keep in mind when using anonymous inner classes: • Anonymous Inner classes cannot have a constructor method. • Anonymous Inner classes cannot define any static members, static methods. • Anonymous inner class cannot be public,protected,private,static. • Only one instance of an anonymous inner class can be created. • An anonymous inner class must be behind new, implementing an interface with its implication or implementing a class. • Because the anonymous inner class is a local inner class, all restrictions on the local inner class are applied to it. A, an inherited anonymous inner classPublic class Car {Public Void Drive () { System.out.println ("Driving a car!"); }Public static void Main (string[] args) { Car car = new car () {Public Void Drive () { System.out.println ("Driving another car!"); } }; car.drive (); } } The result is output: Driving another car! The car reference variable is not a reference car object, but an object of the car anonymous subclass. B, an anonymous internal class of interfaces. interface Vehicle {Public Void Drive (); } class test{Public static void Main (string[] args) { Vehicle v = new Vehicle () {Public Void Drive () { System.out.println ("Driving a car!"); } }; v.drive (); } } The code above is strange, as if it were instantiating an interface. This is not the case, the anonymous inner class of an interface is an anonymous class that implements an interface. And only one interface can be implemented. C, an anonymous inner class of parameters. class bar{ void Dostuff (Foo f) { F.foo (); } } Interface foo{ void foo (); } class test{ static void Go () { Bar B = new bar (); B.dostuff (New Foo () {public void foo () { System.out.println ("Foofy"); } }); } } 4. Static nested classStatic or non-static members can be defined in a statically intrinsic class. Technically, a static nested class does not belong to an inner class. Because an inner class shares a special relationship with an external class, it is more specifically a shared relationship to an instance. Static nested classes do not have the above relationship. It is only positioned inside another class and is therefore also referred to as a top-level nested class. The static meaning is that the inner class can access it just like any other static member without an external class object. Static nested classes can only access static members and methods of external classes. class outer{ static class inner{} } class Test {Public static void Main (string[] args) { outer.inner n = new Outer.Inner (); } }The inner class defined in the static method is also staticnested class, at this time cannot add the static keyword in front of the class, the Staticnested class in the static method is similar to the application of the inner class in the normal method, In addition to the direct access to static member variables in the outer class, it is possible to access local variables in the statically method, but the local variable must be added to the final modifier before it.

Summary of internal class learning in Java

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.