Java static, this, super, final usage (1)

Source: Internet
Author: User
This article aims to help students who are preparing to learn Java and who are new to Java to understand, master, and use the static, this, super, and final keywords. Java is profound and profound. I am also a fan of learning and using Java. It is inevitable that there will be something wrong with this article. please correct me.

I. Static
Take a look at the following program:

  1.  
  2. Public ClassHello {
  3. Public Static VoidMain (String[] ARGs ){// (1)
  4. System. Out. println ("Hello, world! ");// (2)
  5. }
  6. }

I have read this program, and it is no stranger to most of them who have passed Java mathematics. Even if you have never learned Java and other advanced languages, such as C, you should be able to understand the meaning of this Code. It simply outputs "Hello, world", which is useless at all. However, it shows the main usage of the static keyword.
In section 1, we define a static method named main, which means to tell the Java compiler that I can use this method without creating such an object. How do you run this program? In general, we enter the following command in the command line (with the underline as manual input ):
Javac hello. Java
Java hello

Hello, world!
This is the process you run. The first line is used to compile hello. after the Java file is executed, If you view the current file, you will find an additional hello. class file, which is the Java binary bytecode generated in the first line. The second line is the most common practice of executing a Java program. The execution result is as expected. In 2, you may wonder why this is the case. Well, let's break down this statement. (If you have not installed the Java documentation, go to Sun's official website to browse j2se API.) First, the system is located in Java. A core class in the lang package. If you view its definition, you will find a line like this: public static final printstream out. Then, click the hyperlink printstream on the method page, you will see a lot of defined methods, look for println, there will be such a line:
Public void println (string X ). Well, now you should understand why we need to call it like that. Out is a static variable of system, so we can use it directly, and the class to which out belongs has a println method.

Static Method
Generally, a method is defined as static in a class, that is, this method can be called without the objects of this class. As follows:

  1. ClassSimple {
  2. Static VoidGo (){
  3. System. Out. println ("go ...");
  4. }
  5. }
  6. Public ClassCal {
  7. Public Static VoidMain (String[] ARGs ){
  8. Simple. Go ();
  9. }
  10. }

To call a static method is "class name. Method Name". The usage of the static method is very simple as shown above. Generally, static methods are often used to provide some practical tools for other classes in the application. A large number of static methods in Java class libraries are defined for this purpose.
Static variables
Static variables are similar to static variables. All such instances share this static variable. That is to say, only one bucket is allocated during class loading, and all such objects can control this bucket. Of course, final is another option. See the following code:

  1. ClassValue {
  2. Static IntC = 0;
  3. Static VoidINC (){
  4. C ++;
  5. }
  6. }
  7. ClassCount {
  8. Public Static VoidPRT (StringS ){
  9. System. Out. println (s );
  10. }
  11. Public Static VoidMain (String[] ARGs ){
  12. Value V1, V2;
  13. V1 =NewValue ();
  14. V2 =NewValue ();
  15. PRT ("v1.c =" + v1.c + "v2.c =" + v2.c );
  16. V1.inc ();
  17. PRT ("v1.c =" + v1.c + "v2.c =" + v2.c );
  18. }
  19. }

The result is as follows:
V1.c = 0 v2.c = 0
V1.c = 1 v2.c = 1
This proves that they share a storage zone. Static variables are similar to global variables in C. It is worth exploring the initialization of static variables. We modified the above program:

  1. ClassValue {
  2. Static IntC = 0;
  3. Value (){
  4. C = 15;
  5. }
  6. Value (IntI ){
  7. C = I;
  8. }
  9. Static VoidINC (){
  10. C ++;
  11. }
  12. }
  13. ClassCount {
  14. Public Static VoidPRT (StringS ){
  15. System. Out. println (s );
  16. }
  17. Value v =NewValue (10 );
  18. StaticValue V1, V2;
  19. Static{
  20. PRT ("v1.c =" + v1.c + "v2.c =" + v2.c );
  21. V1 =NewValue (27 );
  22. PRT ("v1.c =" + v1.c + "v2.c =" + v2.c );
  23. V2 =NewValue (15 );
  24. PRT ("v1.c =" + v1.c + "v2.c =" + v2.c );
  25. }
  26.  
  27. Public Static VoidMain (String[] ARGs ){
  28. Count Ct =NewCount ();
  29. PRT ("CT. c =" + CT. V. C );
  30. PRT ("v1.c =" + v1.c + "v2.c =" + v2.c );
  31. V1.inc ();
  32. PRT ("v1.c =" + v1.c + "v2.c =" + v2.c );
  33. PRT ("CT. c =" + CT. V. C );
  34. }
  35. }

The running result is as follows:
V1.c = 0 v2.c = 0
V1.c = 27 v2.c = 27
V1.c = 15 v2.c = 15
Ct. c = 10
V1.c = 10 v2.c = 10
V1.c = 11 v2.c = 11
Ct. c = 11
This program demonstrates various static initialization features. If you contact Java for the first time, the results may surprise you. It may be confusing to add braces after static. The first thing to tell you is that static variables take precedence over any other non-static variables, regardless of the order in which they appear. As shown in the program, although V appears before V1 and V2, the result is that V1 and V2 are initialized before v. InStatic {Followed by a piece of code, this is used for explicit static variable initialization, this code will only be initialized once, and when the class is loaded for the first time. If you can read and understand this code, it will help you understand the static keyword. When inheritance is involved, the static variables of the parent class will be initialized first, then the static variables of the Child class, and so on. Non-static variables are not the topic of this article and will not be discussed in detail here. Please refer to think in Java.
Static class
Generally, a common class cannot be declared as static. Only one internal class can be declared. In this case, the static internal class can be directly used as a common class without the need to instance an external class. The following code is used:

  1. Public ClassStaticcls {
  2. Public Static VoidMain (String[] ARGs ){
  3. Outercls. innercls OI =NewOutercls. innercls ();
  4. }
  5. }
  6. ClassOutercls {
  7. Public Static ClassInnercls {
  8. Innercls (){
  9. System. Out. println ("innercls ");
  10. }
  11. }
  12. }

The output results will be as expected:
Innercls
It is the same as a normal class. For other usage of internal classes, see the relevant sections in think in Java.
The other three articles (this, super, final) will be published one after another. Stay tuned.
 

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.