Static explanation in Java

Source: Internet
Author: User

If a class member is declared as static, it can be accessed before any object of the class is created without referencing any object.

The most common example of static members is main (). Because the main () must be called when the program starts execution, it is declared as static.

Variables declared as static are actually global variables. When declaring an object, it does not produce a copy of the static variable, but all instance variables of this class share the same static variable,

For example, declare a static variable count as the Count of a new class instance.

Methods declared as static have the following restrictions:

1. They can only call other static methods.

2. They can only access static data.

3. They cannot reference this or super in any way.

If you need to initialize your static variable through calculation, you can declare a static block,

The static block is executed only once when the class is loaded. The following example shows that the class has a static method, some static variables, and a static initialization block:

Public class userstatic {

Static int A = 3;

Static int B;

Static void meth (int x ){

System. Out. println ("x =" + x );

System. Out. println ("A =" + );

System. Out. println ("B =" + B );

}

Static {

System. Out. println ("static block initialized .");

B = A * 4;

}

Public static void main (string ARGs []) {

Meth (42 );

}

}

Once the usestatic class is loaded, all static statements are run.

First, a is set to 3, then the static block is executed (print a message), and finally, B is initialized to a * 4 or 12. Then, call main () and main () to call meth () and pass the value 42 to X. The three println () Statements reference two static variables A and B, and the local variable X.

Note: It is invalid to reference any instance variable in a static method.

The output of the program is as follows:

Static block initialized.
X = 42
A = 3
B = 12

Outside the class that defines them, static methods and variables can be used independently of any object,

You can call static variables and static methods by class name.

In this way, you only need to add the number (.) Operator after the class name. For example, if you want to call a static method from outside the class, you can use the following common format:

Classname. Method ()

Here, classname is the class name and defines the static method in this class. As you can see, this format is similar to that used to call non-static methods through object reference variables.

A static variable can access the -- class name plus vertex operator in the same format. This is a control version of how Java implements global functions and global variables.

Summary:
1. Static members cannot be accessed by instances created by their class.

2. If a Member without static modification is an object member, it is owned by each object.

3. The static modified member is a class member, which can be called directly by a class and is shared by all objects.

Java static: a modifier that can be used to modify variables, methods, and code blocks (but cannot modify classes ).

1. Modify variables:

An attribute that all objects of a class share. It is also called a class variable. This is similar to the global variable in C. Class variables are initialized only once during class loading. Any object in the program modifies static variables. Other Objects see the modified values. Therefore, class variables can be used as counters. In addition, Java static variables can be directly accessed by class names without requiring objects.

2. modification method:

A function shared by all objects of a class is called a static method. Static methods can also be accessed directly by class names without the need for objects. Therefore, you cannot directly access non-static variables and non-static methods in static methods. keywords such as this or super cannot appear in static methods.

3. Modify the Java code block:

Use static to modify an independent code block in the class, which is called a static code block. The static code block is executed only once when the class is loaded for the first time. The static code block has no name, so it cannot be called explicitly, but only called by virtual machines during class loading. It is mainly used to complete some initialization operations.

4. class loading:

When JVM uses a class for the first time, it will go to the path specified by classpath to find the bytecode file corresponding to the class and read it into JVM for saving. This process is called class loading.

It can be seen that, whether it is a variable, method, or code block, as long as static modification is used, it is "ready" when the class is loaded, that is, it can be used or executed. Can be executed without objects. Otherwise, if there is no static object, you must access it through an object.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/sniperwang/archive/2009/10/29/4744753.aspx

Public class straticmethod {
Static int A = 3;
Static int B;

Static void meth (int x ){
System. Out. println ("x =" + x );
System. Out. println ("A =" + );
System. Out. println ("B =" + B );

}
Static {
System. Out. println ("static block initialized .");
B = A * 4;

}
// Static code block, which cannot be displayed and called.
Public static void main (string ARGs []) {
Meth (42 );
}
// When the program is executed, the static part is first executed. The initial value is assigned to a, B (B = 0, no default value is set to 0), and the static part of the code block is executed.
// But the static method is not executed, but it can be called by other classes without initializing objects.
}

 

My code below (not reprinted)

Package COM. statictest; public class statictestclass {public static string FF = "QQ"; Public String ff2 = "qq2"; public static void main (string [] ARGs) {// todo auto-generated method stubstatictestclass cc = new statictestclass (); statictestclass. mainee ("jjjkkk"); system. out. println (CC. ff); system. out. println (statictestclass. ff);} public static void mainee (string SS) {// only static variables and methods can be called in static blocks. // call static Variable statictestclass. FF + = SS; // call the static method statictestclass. sys2 () in the static block; // you cannot use this in the static block! // This .} public void sys () {system. out. println ("ll");} public static void sys2 () {system. out. println ("ll -- Static");} static {system. out. println ("I was executed first ");}}

Output:

I was first executed LL -- staticqqjjjkkkqqjjjkkk

 

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.