Java static functions

Source: Internet
Author: User

Java static functions

Static indicates the meaning of "global" or "static". It is used to modify member variables and member methods. It can also form static code blocks, but Java does not have the concept of global variables. The static modified member variables and member methods are independent of any object in the class. That is to say, it is shared by all instances of the class and does not depend on a specific instance of the class. As long as the class is loaded, the Java Virtual Machine can locate the class names in the Method Area of the runtime data zone. Therefore, a static object can be accessed before any of its objects are created without referencing any objects. Static member variables and member Methods Modified with public are essentially global variables and global methods. When the object city of the declared class does not generate a copy of static variables, instead, all instances of the class share the same static variable. The static variable can be modified in private, indicating that the variable can be in the static code block of the class, or other static member methods of the class (you can also use -- nonsense in non-static member methods), but it cannot be directly referenced by the class name in other classes, this is important. In fact, you need to understand that private is the access permission limitation, and static means that it can be used without instantiation, so that it is much easier to understand. The effect of adding other access key words before static is also similar. Static modified member variables and member methods are usually called static variables and static methods. They can be accessed directly by class names. The access syntax is: Class Name. static Method Name (parameter list ...) class Name. static variable names use static modified code blocks to represent static code blocks. When a Java Virtual Machine (JVM) loads a class, the code block is executed (very useful ). 1. static variables can be classified into two types based on static variables: static variables, static variables or class variables; the other is a variable that is not modified by static, called an instance variable. The difference between the two is: for static variables with only one copy in the memory (memory saving), the JVM only allocates memory for static variables once, and completes the memory allocation of static variables during the loading process, you can directly access the available class name (convenient). Of course, you can also access it through an object (but this is not recommended ). If an instance variable is not created, the instance variable will be allocated with memory once. The instance variables can be copied multiple times in the memory without affecting each other (flexible ). Therefore, when you need to implement the following two functions, use static variables: When values are shared between objects, use static variables to facilitate variable access. 2. Static static methods can be called directly by class names, any instance can also be called, so the keyword "this" and "super" cannot be used in static methods, you cannot directly access the instance variables and instance methods of the class (that is, the static member variables and member methods are not included). You can only access static member variables and member methods of the class. Because instance members are associated with specific objects! You need to understand the truth, not the memory !!! Because the static method is independent of any instance, the static method must be implemented rather than abstract. For example, to facilitate method calling, all the methods in the Math class in Java API are static, while the static method in the general class is also convenient for other classes to call this method. Static methods are a special class of internal methods. They are declared as static only when needed, internal methods of a class are generally non-static. 3. static code blocks are also called static code blocks. They are static statement blocks independent of Class Members in the class and can contain multiple, positions can be placed randomly. It is not in any method body. When JVM loads classes, these static code blocks are executed. If there are multiple static code blocks, JVM will execute them sequentially according to the order they appear in the class, and each code block will be executed only 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);} public static void main (String [] args) {// TODO Automatic Generation Method stub} static {Test5.a = 5; System. out. println (a);} public void f () {System. out. println ("hhahhahah ");}}

Running results: 3 hhahhah 1000 4 5 using static code blocks, you can assign values to some static variables. Finally, let's take a look at these examples, all of which are static main methods, in this way, the JVM can directly call the main method without creating an instance. 4. static and final are used to indicate what static final is used to modify member variables and member methods. They can be simply understood as "Global constants "! For a variable, it means that once the value is given, it cannot be modified and can be accessed through the class name. For methods, it means they cannot be overwritten and can be accessed directly by class names. Sometimes you want to define a class member so that its use is completely independent of any object of the class. Generally, a class member must be accessed through its class object, but can create such a member, which can be used by itself without referencing a specific instance. Add the keyword static before the declaration of the member to create such a member. If a member is declared as static, it can be accessed before any object of its class is created without referencing any object. You can declare both methods and variables as static. 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. The Methods declared as static have the following restrictions :? They can only call other static methods. ? They can only access static data. ? They cannot reference 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 through calculation, you can declare a static block, which is only executed 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: // 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 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 following is the output of the program: 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. 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 generic 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. The following is an example. In main (), the static methods callme () and static variables B are accessed outside of 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 = 42 B = 99 static members cannot be accessed by the instance created by their class. If a member without static modification is an object member, it is owned by each object. The member with static modification is a class member, which can be directly called by a class and is shared by all objects.

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.