Java static internal class and non-static internal class learning records.

Source: Internet
Author: User

Java static internal class and non-static internal class learning records.
Purpose

Why is there this article? I found that many frameworks use these internal skills when studying various frameworks. Although I do not usually need to write code, however, if you look at other people's code, you should at least understand the basic knowledge. In addition, I am not very clear about the situations where internal classes should be applied, and leave some questions worth thinking about as a record, maybe you can fill it out by yourself later. So we will have this article.

I don't want to introduce the general usage method either. I will talk about what is easy and worth noting on the Internet.

 

Note:

This article only shares some of my research and confusions on internal departments. It may be helpful to everyone and may guide everyone to think and learn together. Java syntax knowledge is complex and easy to make mistakes, especially in terms of details. So my point may be wrong.

In addition, my JDK version is 1.7. The JDK1.8 version seems to have some modifications to the internal Department class rules, so please note that.

 

Non-static non-Anonymous internal class cannot have static members

In most articles on the Internet, this is to tell you that such internal classes cannot have static methods and attributes. In fact, this sentence has some minor problems. Non-static internal classes can also have static attributes (static blocks do not work), but this static attribute must be a constant. After my research, I think it is similar to "calling the static attribute of the class, but it does not initialize the static block of the class and other static variables. What does it mean? That is to say

1 class Outter{2     class Inner{3         final static int a = 9; 4     }5 }

There is no compilation error. Static variable a must be of the final type, basic type, and nominal value.

1 class Outter{2     class Inner{3         final static int a = get(); 4     }5     6     static int get(){7         return 1;8     }9 }

In this case, an error is reported because a is not a literal value.

Here I would like to take a look at my other learning record. Such variables will not trigger class initialization.

Address: The second dot under the http://www.cnblogs.com/abcwt112/p/4567332.html analysis title.

Of course, this kind of nominal value seems useless .... So it seems that this internal class cannot have static members ..

Why cannot I own static members?

I think this is the case (personal understanding ). Non-static internal classes are similar to common members and belong to external class objects. To create such internal class objects, you must first create external class objects. That is to say, this internal class cannot exist independently of the external class object. Everyone understands how a member variable exists independently from the object of the class. Assume that this internal class can have static attributes, such as static int a = 1. Where is the existence of this a data? According to the previous analysis, it should exist in the memory allocated to external class objects. After an external class object is destroyed, the memory allocated by a will also be destroyed. In this case, it is meaningless to modify this a because the memory is re-allocated each time and the value is 1 each time. So this static is useless and not global.

This is my understanding. I think that when I finish reading the JVM Virtual Machine (I think it is possible that the final static int and static int variable values may be stored differently) or I will learn the Assembly (I can observe how the data is stored) you may have a deeper understanding of it later.

 

Static internal classes can only call static methods and attributes of external classes

This is quite understandable, just as the static method main cannot call non-static attributes. However, this does not mean that static internal classes cannot define non-static methods and attributes. It cannot call non-static members of an external class, but can be defined within itself. After all, it is a class ....

 1 public class StaticTest { 2     static int a = 9; 3      4     private static class Inner{ 5         public static void main(String[] args) { 6             System.out.println(StaticTest.a); 7         } 8          9         public void doSomething(){10             11         }12     }13     public static class Inner2{14     }15 }

There is no problem with the code here. It can be compiled through (doSomething method ).

Usage (nonsense)

Static internal classes do not point to external class references, so it has nothing to do with external classes. Therefore, even if there is no external class object, you can also directly create static internal class objects. However, I think that if this is the case, there is no need to make the class static and internal, and it is OK to directly create the general class of the external class. Therefore, static internal classes must have their own unique functions. Since it is an internal class, I think it should be declared as private, otherwise it would be better to create an external class .... So what is the use of private static class? I don't know... The only thing that I think is useful now is to write the main method of the test code in it. Because it is a private class, the external side cannot call it, so it cannot call the written main function .. However, you can use junit test for testing only... It is impossible to develop such a syntax for testing purposes... This is worth studying in the future. When I read more code, I may have a new experience.

 

 

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.