Java static usage

Source: Internet
Author: User

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 ):
Java
C 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, that is the first line of production
Java binary bytecode. 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. Okay. Let's break it down.
Click this statement. (If you have not installed the Java documentation, go to Sun's official website to view j2se
API) First, system is a core class located in the Java. lang package. If you view its definition, you will find a line like this: public static
Final printstream
Next, click the printstream hyperlink. On the Method page, you will see a large number of defined methods, look for println, and there will be a line like this:
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
Programs demonstrate 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. First, let me know.
Yes, 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
But V1 and V2 are initialized before v. A piece of code is followed after static {, which is used for explicit static variable initialization. This code will only be initialized once and will be loaded for the first time in the class.
. 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 are initialized first, and then the static variables of the Child class are sorted by class.
Push.
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.