Understanding Static (Learn more about Java Virtual machines)

Source: Internet
Author: User
Tags modifier

Talk to me aboutStaticThe understandingby~~~~~~~~~~~~~~~~~~ Slag Fly(Secretly use my paper to draw the calf to break the fracture ~la la la ~Because I found out that a lot of students will be very confused. It's hard to understand what static is. First static is a variable modified by the static modifier we collectively refer to as statically variable also called class variable (why is it called a class variable?) Because we can directly use the class to invoke the static modified variable)static can not modify local variables! Remember to tell us again. Another variable is also a member variable called but not modified by static we call it the global variable is also a dynamic variable in many people's mouths (called in c) I've got a much easier way of remembering it. Instance variable is non-static variable (object is not unfamiliar to class instantiation) Instead of a static variable you want to call when you need an object so I call it an instance variable. There is only one copy of the static variable in memory! The JVM will only assign static memory static variables once the class is loaded for non-static variables that can be accessed through objects! Every new instance of your JVM will assign you a memory instance variable that can have multiple copies in memory without affecting each other (flexibility) the storage of static variables in RAM is stored in the datastore and the stack is not a storage area plug in a mouth to talk about heap and stackThe following students do not understand can not read the first! Stack space small speed relatively fast used to put object references
Heap general all created objects are placed here

Stack: is an advanced out-of-the-box data structure typically used to save a method (c: function) in a parameter local variable eight basic types and a reference to a custom object (c: Pointer) The method that is executed will go out of the stack in Java all the base types and reference types are stored in the stack The living space of the data in the stack is usually within the current scopes (that is, by {...} Heap (heap): is a dynamically requested memory space (its list of free memory spaces is maintained by the operating system), and the memory space generated by the malloc statement in C is in the heap. In Java, all objects constructed with new XXX () are stored in the heap, and the object is automatically destroyed when the garbage collector detects that an object is not referenced. So, theoretically, there is no limit to the living space of an object in Java, so long as a reference type points to it, it can be used anywhere.

1. Stacks and heaps (heap) are places that Java uses to store data in RAM. Unlike C + +, Java automatically manages stacks and heaps, and programmers cannot directly set up stacks or heaps.
2. The advantage of the stack is that the access speed is faster than the heap, second only to the registers directly in the CPU. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. In addition, the stack data can be shared. The advantage of the heap is that the memory size can be allocated dynamically, and the lifetime does not have to tell the compiler beforehand that the Java garbage collector automatically collects the data that is no longer in use. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time. 3. There are two kinds of data types in Java. One is the basic type (primitive types), a total of 8 kinds, namely int, short, long, byte, float, double, Boolean, char (note, and no basic type of string). The definition of this type is through such as int a = 3; Long B = 255L; the form to be defined, called an automatic variable. It is worth noting that the automatic variable is a literal value, not an instance of a class, that is not a reference to a class, there is no class here. such as int a = 3; Here A is a reference to the int type, pointing to the literal value of 3. The data of these literals, due to the size of the known, the lifetime of the known (these values are fixed in a program block, the program block exits, the field value disappears), for the sake of speed, it exists in the stack. In addition, the stack has a very important particularity, is that there is data in the stack can be shared. Suppose we define both: int a = 3;
int b = 3; the compiler processes int a = 3 First, it creates a reference to a variable in the stack, and then looks for an address with a literal value of 3, and then finds an address that holds the literal value of 3, and then points A to the address of 3. then the int b = 3 is processed, and after the reference variable of B is created, B is pointed directly to the address of 3 because there are already 3 literals in the stack.
In this case, A and B both point to 3. It is particularly important to note that the reference to this literal is different from the reference to the class object.
Assuming that a reference to two class objects points to an object at the same time, if an object reference variable modifies the internal state of the object, then another object reference variable will immediately reflect that change.
Conversely, modifying its value by a reference to a literal value does not result in another case where a reference to that literal is changed. As in the example above, we define the value of a and B and then make a=4; then B will not be equal to 4 or equal to 3. Inside the compiler, when it encounters A=4, it will re-search the stack for a literal value of 4, and if not, re-open the value of the address 4, and if so, point a directly at the address. Therefore the change of a value does not affect the value of B. The other is the wrapper class data, such as Integer, String, double, and so on, the corresponding basic data types are wrapped up class. These classes of data all exist in the heap, and Java uses the new () statement to tell the compiler that it is dynamically created as needed at run time, so it is more flexible, but the disadvantage is that it takes more time. 4.String is a special packing class data. That can be created in the form of string str = new String ("abc"), or in the form of string str = "ABC";now back to the static we're talking aboutstatic modified variables The following is the method of static modificationWhat are the benefits of static methods? Static methods do not require a class instantiation object to directly invokewe said in the heap and the stack that the object is a much more memory-intensive thing.in other words, the method that is modified by static is no longer owned by the object but belongs to the class of course. Or it can be called it seems unreasonable to me! Recommended use of the class name is not recommended if you see a method or variable called directly by the class name then he must be static.and everyone said that static of the drawbacks of static will be from your JVM load class began to exist until the end of the entire program will be released! so please use it with caution!  static I personally like to use him as a tool because you don't need a new object to be used directly, for example, if we want to connect to a database later, we can use the static modifier because connecting to a database is not unique to an object. He's just a tool.Finally, let's talk about a static modified code block.the difference between static code blocks and static methods is thatstatic code blocks are executed automatically and the method is executed when you call himStatic methods can only invoke other static members of the class directly (variable methods) static cannot be called non-static (get on the car after boarding) in the JVM is static decoration will be loaded first and this time the other is not static decoration is not loaded so it is not calledOK, tired = = Don't write yourself more understand not to take the time to ask mehere is a copy of the JVM3 I talked about as a storage area =. = self-Understanding

 

 

Understanding Static (Learn more about Java Virtual machines)

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.