"Wow" internal class (3) ---- nested class

Source: Internet
Author: User

  Can internal Classes define static data or methods in their visible domains? The answer is no. Why? Think about it. static data and methods are called class data and class methods. Why? Because they are an integral part of a class and are equivalent to a method at a level, internal classes cannot possess static data and methods, just as the method cannot have static. But Nested classes can. What is a nested class? A nested class is an internal class with static modification. The behavior of the nested class is very different from that of the internal class. It is basically different from that of the internal class, it is an internal class, so the nested class can indeed be called an internal class. The biggest difference between Nested classes is that they are not referenced by peripheral classes! When talking about static, we have said that static does not need to be associated with a specific class object. Therefore, you can directly create Nested classes without creating external class objects, naturally, the reference of the peripheral class is lost. So why do we need Nested classes? A very important application is to use Nested classes in interfaces. Many people may wonder why Nested classes can be placed in interfaces? The methods in the interface are not implemented in detail, but the nested classes are different. They can have specific implementations. Code . But this is actually not a conflict. Any class in the interface is public and static, and the nested class itself meets such requirements, so it does not violate the interface naming rules. So why should we place Nested classes in interfaces? Because Nested classes can have their own specific implementations, or even implement external interfaces, and these can be used by the classes that implement this interface, the role of Nested classes is also desired. If you want some public code to be shared by different implementations of the interface, you can use Nested classes in the interface. For example, we all want to test the classes we designed when creating classes. One way is to put the main () method in each class we designed, however, this method will be re-compiled every time a class instance is created, so it is not appropriate to put the main () method in a nested class, there is no such concern, because we all know that static is only compiled once. This is also a good use case for nested classes.

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.