Use of static in Java

Source: Internet
Author: User

1. Static methods
In general, defining a method in a class is static, which means that you do not need an object of this class to call this method
The method declared as static has the following limitations:
• They can only invoke other static methods.
• They can only access static data.
• They cannot refer to this or super in any way.
Class Simple {
Staticvoid Go () {
System.out.println ("Welcome");
}
}

Publicclass Cal {
Publicstaticvoid Main (string[] args) {
Simple.go ();
}
}
Invoking a static method is "class name. Method Name", and the use of static methods is simple as shown above. In general, static methods are often used by other classes in the application, and in Java's class libraries a large number of static methods are defined for this purpose.

2. Static variables
A variable declared as static is essentially a global variable. When declaring an object, the copy of the static variable is not produced, but all instance variables of the class share the same static variable. Static variables are similar to static methods. All such instances share this static variable, that is, when a class is loaded, only one storage space is allocated, and all objects of this class can manipulate this block of storage space, of course, for final.
Class Value {
STATICINTC = 0;

Staticvoid Inc () {
C + +;
}
}

Publicclass Count2 {
Publicstaticvoid prt (String s) {
System.out.print (s);
}

Publicstaticvoid 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: v1.c=0 v2.c=0 v1.c=1 v2.c=1
This can prove that they share a piece of storage. The static variable is somewhat similar to the concept of a global variable in C.

It is worth discussing the initialization problem of static variables.
If you need to initialize your static variable with a calculation, you can declare a static block that executes only once when the class is loaded. The following example shows a class that has a static method, some static variables, and a static initialization block:
Class Value3 {
STATICINTC = 0;

Value3 () {
c = 15;
}

VALUE3 (int i) {
c = i;
}

Staticvoid Inc () {
C + +;
}
}

Publicclass Count {
Publicstaticvoid prt (String s) {
System.out.println (s);
}

Value3 v = new Value3 (10);
Static VALUE3 v1, V2;
static {//This is a static block
PRT ("v1.c=" + v1.c + "v2.c=" + v2.c);
V1 = new Value3 (27);
PRT ("v1.c=" + v1.c + "v2.c=" + v2.c);
v2 = new Value3 (15);
PRT ("v1.c=" + v1.c + "v2.c=" + v2.c);
}

Publicstaticvoid 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 result is:
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 shows the various features of static initialization. If you touch Java for the first time, the results may surprise you. It may be confusing to add parentheses after static. The first thing to tell you is that the variables defined by static take precedence over any other non-static variables, regardless of the order in which they appear. As shown in the program, although v appears in front of V1 and v2, the result is the initialization of V1 and V2 in front of v. followed by a piece of code behind static{, which is used for explicit static variable initialization, which is initialized only once, and when the class is first loaded. If you can read and understand this code, it will help you to understand the static keyword. When it comes to inheritance, the static variables of the parent class are initialized first, then the subclasses, and so on.

3. Static class
Usually a normal class is not allowed to be declared static, only one inner class can. In this case, the static inner class can be used directly as a normal class, without the need to instantiate an external class.
Publicclass Staticcls {
Publicstaticvoid Main (string[] args) {
Outercls.innercls oi = newoutercls.innercls ();
}
}

CLASSOUTERCLS {
Publicstaticclass Innercls {
Innercls () {
System.out.println ("Innercls");
}
}
}
The result is: innercls

Use of static in Java

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.