Differences between static nested class and inner class

Source: Internet
Author: User

Nested class (generally C ++) and inner class (generally Java ). The biggest difference between Java internal classes and C ++ Nested classes is whether there are external references.

Note: static internal class (inner class) means

1. Create a static internal class object without an external Class Object

2. You cannot access an external class object from an object of a static internal class.

Can anonymous inner class (anonymous internal class) be extends (inherited) other classes, and implements (implemented) interface (Interface )?

Inner class (internal class) is defined in the class.
Nested class (nested class) is a static internal class. 1. To create nested class objects, you do not need the objects of its peripheral class. 2. Non-static peripheral class objects cannot be accessed from nested class objects.

Anonymous inner class (anonymous internal class) Anonymous internal class is an internal class without a name.

Anonymous internal classes cannot be extends (inherited) other classes, but an internal class can be used as an interface and implemented by another internal class.

Nested classes can be used as internal classes of interfaces. Normally, you cannot place any code inside the interface, but the nested class can be part of the interface because it is static. Only placing Nested classes in the interface namespace does not violate the interface rules.

The internal class is inherited. Because the internal class has a secret reference pointing to the peripheral class object, this secret reference must be initialized when inheriting the internal class. The solution is enclosingclassreference. Super (); Syntax. Let's take a look at the Code:

Class Outer
...{
Class inner
...{
}
}
Class anoclass extends outer. Inner
...{
Anoclass (outer WI)
...{
WI. Super ();
}
}

Anonymous class)
When the class name of an internal class is used only once when such an object is created, and the new class to be generated must inherit from an existing parent class or implement an interface, in order to consider using an anonymous class, the anonymous class itself is unknown, so it does not have a constructor. It needs to explicitly call the constructor of a parent class without parameters, and override the method of the parent class.

............

F. addmousemotionlistener (New mousemotionadapter () {// Anonymous class starts
Public void mousedragged (mouseevent e ){
String S = "Mouse dragging: x =" + E. getx () + "Y =" + E. Gety ();
TF. settext (s );}
}); // End of the anonymous class

The reason is:
1. An internal class object can access the implementation of the object created for it, including private data. That is, an internal class instance is privileged to the instance of the class that contains it.
2. for other classes in the same package, the internal class can be hidden. In other words, the internal class, regardless of the method visibility, may be public. In addition to the inclusive class, other classes cannot use it.
3. Anonymous internal classes can easily define callbacks.
4. Use internal classes to easily write event drivers.
In fact, it is only intended to define callback-further, it is event-driven.

When using anonymous internal classes, remember the following principles:
· No constructor is allowed for anonymous internal classes.
· Anonymous internal classes cannot define any static members, methods, and classes.
· Anonymous internal classes cannot be public, protected, private, or static.
· Only one instance of the anonymous internal class can be created.
· An anonymous internal class must be behind New and be used to implicitly implement an interface or implement a class.
· Because the anonymous internal class is a local internal class, all restrictions on the local internal class take effect.

This in the anonymous class and internal class:
Sometimes, some internal and anonymous classes are used. When this is used in an anonymous class, this refers to the anonymous class or internal class itself. If we want to use external class methods and variables, we should add the Class Name of the external 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.