Talking about: Java static variables and static methods. __java

Source: Internet
Author: User
Tags modifier
static denotes "global" or "static" meaning, used to decorate member variables and member methods, or to form static-code blocks, but the concept of global variables is not available in the Java language. The member variables and member methods that are decorated by static are independent of any object of the class.

That is, it does not rely on class-specific instances and is shared by all instances of the class. As long as this class is loaded, the Java virtual machine can find them based on the class name in the method area of the runtime data area. 

Therefore, a static object can be accessed before any of its objects are created without referencing any objects. 

Static member variables and member methods that are decorated with public are both global and global methods, and when declaring the object city of its class, a copy of the static variable is not generated, but all instances of the class share the same static variable. A static variable can have a private modifier before it it is important to indicate that this variable can be used in a static code block of a class, or in other static member methods of a class (which can, of course, be used in non-static member methods-nonsense), but cannot be referenced directly by the class name in other classes. In fact, you need to understand that private is the access permission limit, static means not instantiated can be used, so easy to understand more. 

The effect of the preceding static plus the other access rights keyword is also so. 
The member variables and member methods of the static modifier are customarily called static variables and static methods, which can be accessed directly through the class name, which is the class name. static method Name (parameter list ...) 

Class name. Static variable name a static-decorated block of code that represents a block of code that executes when the Java Virtual Machine (JVM) loads the class (very useful, hehe).

1, static variable according to whether static to classify class member variable can be divided into two kinds: one is the variable modified by static, which is called statically variable or class variable; the other is a variable not modified by static, called an instance variable. 
 The difference between the two is that there is only one copy of the static variable in memory (save memory), the JVM allocates only one memory for the static, completes the memory allocation of the static variables during the load class, can be accessed directly by the class name (convenient), and, of course, through the object (but this is not recommended). 

For instance variables, each time an instance is created, the instance variable is allocated memory once, and the instance variable can have more than one copy in memory, with no effect (flexibility). So you typically use static variables when you need to implement the following two features:  when sharing values between objects to facilitate access to variables 2, static method static method can be called directly through the class name, any instance can also be invoked, so the static method cannot use this and Super keyword, can not directly access theInstance variables and instance methods of generic classes (that is, member variables and member methods without static) can only access static member variables and member methods of the owning class. Because instance members are associated with a particular object. 
This need to understand, to understand the truth, not memory ...

Because the static method is independent of any instance, the static method must be implemented, not abstract.

For example, for easy invocation of the method, all methods in the math class in the Java API are static, while the static method inside the generic class is also convenient for other classes to invoke the method. Static method is a kind of special method inside class, only when necessary to declare the corresponding method static, a class internal methods are generally non-static 3, static code block static code block is also called static code block, is independent of class members in the class static statement block, can have multiple, The location can be placed casually, it is not in any method body, the JVM executes these static code blocks when the class is loaded, and if there are multiple static code blocks, the JVM executes them sequentially in the order in which they appear in the class, and each block of code is executed once. 
For example: public class Test5 {private static int A; 

private int B; 
static{test5.a=3; 
System.out.println (a); 
Test5 t=new Test5 (); 
T.F (); 
t.b=1000; 
System.out.println (T.B); 
} static{test5.a=4; 
System.out.println (a); 
The public static void main (string[] args) {//TODO automatically generates method stubs} static{test5.a=5; 
System.out.println (a); 
public void F () {System.out.println ("Hhahhahah"); Results: 3 Hhahhahah 1000 4 5 using static code blocks to assign values to some static variables, a final look at these examples is a static main method so that the JVM can invoke the main method directly without 

Create an instance. 4. The static and final piece is used to decorate the member variable and member method with what is static final.Can be easily understood as "global constants." 
For a variable, it means that once the value is not modifiable, it can be accessed through the class name.

For methods, they are not overwritten and can be accessed directly through the class name. Sometimes you want to define a class member so that its use is completely independent of any object of that class. Typically, a class member must be accessed through the object of its class, but you can create a member that can be used by itself without reference to a particular instance. You can create such a member by preceding the declaration of the member with the keyword static (static). If a member is declared static, it can be accessed before any object of its class is created without reference to any object. You can declare both the method and the variable as static. The most common example of a static member is main (). 

Because main () must be invoked when the program starts executing, it is declared static. A variable declared as static is essentially a global variable. When an object is declared, it does not produce a copy of the static variable, but all instance variables of the class share the same static variable. 
The methods declared as static have the following limitations: • They can only invoke other static methods. 
• They can access only static data. 
• They cannot refer to this or super in any way (the keyword super is related to inheritance, as described in the next chapter). If you need to initialize your static variable by calculation, you can declare a static block that executes only once when the class is loaded. 

The following example shows a class with a static method, some static variables, and a static initialization block://demonstrate static Variables,methods,and blocks. 
Class Usestatic {static int a = 3; 

static int b; 
static void meth (int x) {System.out.println ("x =" + x); 
System.out.println ("a =" + 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 executes (prints a message), and finally, B is initialized to a*4 or 12. Then call Main (), and main () calls meth (), passing the value 42 to X. 

3 println () statements refer to two static variables A and B, and local variable x. 

Note: It is illegal to refer to any instance variable in a static method. 
The following is the output of the program: Static block initialized. x = 3 B = 12 The static method and variable can be used independently of any object outside of the class in which they are defined. So you just add the number operator to the name of the class. For example, if you want to invoke a static method from outside of a class, you can use the following generic format: Classname.method () Here, ClassName is the name of the class in which the static method is defined. As you can see, this format is similar to the format of calling a non-static method with an object reference variable. A static variable can be accessed in the same format-the class name is dotted with the number operator. 

This is a controlled version of how Java implements global functionality and global variables. Here is an example. 

In main (), the static method CallMe () and static variable B are accessed outside their classes. 
Class Staticdemo {static int a = 42; 
static int b = 99; 
static void CallMe () {System.out.println ("a =" + a); 
} class Staticbyname {public static void main (String args[]) {Staticdemo.callme (); 
System.out.println ("b =" + STATICDEMO.B); 

The following is the output of the program: a = b = The static member is not accessible by an instance created by its class. 

If a member that is not modified by static is an object member, it is owned by each object. A member of a static modifier is a class member, which can be called directly by a class, and is shared by all objects
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.