Java static usage

Source: Internet
Author: User

I. static
Take a look at the following program:
Public class Hello {
Public static void main (String [] args) {// (1)
System. out. println ("Hello, world! "); // (2)
}
}
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 is just a simple output of "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:
Class Simple {
Static void go (){
System. out. println ("Go ...");
}
}
Public class Cal {
Public static void main (String [] args ){
Simple. go ();
}
}
To call a static method is "class name. Method Name". The usage of the static method is as follows. 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:
Class Value {
Static int c = 0;
Static void inc (){
C;
}
}
Class Count {
Public static void prt (String s ){
System. out. println (s );
}
Public static void main (String [] args ){
Value v1, v2;
V1 = new Value ();
V2 = new Value ();
Prt ("v1.c =" v1.c "v2.c =" v2.c );
V1.inc ();
Prt ("v1.c =" v1.c "v2.c =" v2.c );
}
}
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:
Class Value {
Static int c = 0;
Value (){
C = 15;
}
Value (int I ){
C = I;
}
Static void inc (){
C;
}
}
Class Count {
Public static void prt (String s ){
System. out. println (s );
}
Value v = new Value (10 );
Static Value v1, v2;
Static {
Prt ("v1.c =" v1.c "v2.c =" v2.c );
V1 = new Value (27 );
Prt ("v1.c =" v1.c "v2.c =" v2.c );
V2 = new Value (15 );
Prt ("v1.c =" v1.c "v2.c =" v2.c );
}
Public static void main (String [] args ){
Count ct = new Count ();
Prt ("ct. c =" ct. v. c );
Prt ("v1.c =" v1.c "v2.c =" v2.c );
V1.inc ();
Prt ("v1.c =" v1.c "v2.c =" v2.c );
Prt ("ct. c =" ct. v. c );
}
}
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. The static {is followed by a piece of code, which 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.
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:
Public class StaticCls {
Public static void main (String [] args ){
OuterCls. InnerCls oi = new OuterCls. InnerCls ();
}
}
Class OuterCls {
Public static class InnerCls {
InnerCls (){
System. out. println ("InnerCls ");
}
}
}
The output results will be as expected:
InnerCls
Define a method as static in a class. That is to say, this method can be called without the objects of this class.
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 are similar to static variables. All such instances share this static variable. That is to say, only one bucket is allocated during class loading. All such objects can control this bucket.
Static class: A common class cannot be declared as static. Only one internal class is allowed. In this case, the static internal class can be directly used as a common class without the need to instance an external class.
When you define a variable in a class and add the final keyword before it, this variable cannot be changed once initialized.
If you declare the method as final, it means that you already know that the function provided by this method has met your requirements and does not need to be extended, this method cannot be overwritten by any class inherited from this class, but inheritance can still inherit this method, that is, it can be used directly.

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.