Final static Depth resolution

Source: Internet
Author: User

function test () {var a = b = 10;  } test (); alert (b);

From the previous blog, the final output is 10. The cause of ambiguity is the relationship between global variables and local variables. Follow this topic and talk about this piece of java. Total----the general structure to describe.

Reference blog:

Static detailed analysis

Instance Builder is not a static method

Total: There is no concept of global variables in Java, only local variables and member variables two statements.

There is no concept of "global variables" in Java. But there is the concept of "static". 1.static decorated member variable. 2.static modifies the member method. 3.static modifies a static block of code.
member variables and member methods that are modified by static are independent of any object of the class. That is, it is not dependent on an instance of the class and is shared by all instances of the class. As long as this class is loaded, the Java virtual machine can find them within the method area of the runtime data area based on the class name. Therefore, a static object can be accessed before any of its objects, without applying any objects.
A.

Next, compare final.

The Java.lang.String class cannot be inherited because it is modified by the final. 1.final Modified class, 2.final modification method, 3.final modifier variable ( member variable and local variable ).
The final decorated class, the class cannot be inherited, is the case of the string class. The final modification method, locking the method, prevents inheriting its class to modify its meaning. Final modified variables, which are divided into modified basic data type variables and application type variables. A variable of the base data type cannot be changed after it is modified, and a variable of the reference type cannot point to another object after initialization, but it can change the contents of the current object.

Opinions vary

1. ( National Wealth ) The non-static method cannot be called inside the static method, which in turn is possible.

2. Static purpose is to make it easy to invoke (method/variable) without creating an object. Methods or variables modified by the static keyword do not need to be accessed by objects, as long as the class is loaded, it can be accessed through the class name.

3. For a static method, there is no this, because it is not attached to any object, since there is no object, it is not.

4. Non-static member variables and non-static member methods of a class cannot be accessed in a static method because non-static member methods/variables must be dependent on a specific object to be called.

5. Non-static member methods and non-static member variables cannot be accessed in a static method, but static member methods/variables can be accessed in non-static member methods.

6. The constructor of a class is actually a static method even if it is not declared as static. But why in the construction method? You can also use this to access member variables?? Blog

  Add: Although static member variables are independent of objects, they do not mean that they cannot be accessed through objects, and all static and static variables can be accessed through objects (as long as access rights are sufficient).

7. The difference between a static variable and a non-static variable is that a static variable is shared by all objects, only one portion in memory, and is initialized only when the class is first loaded. The non-static variable is owned by the object and is initialized when the object is created, there are many parts, and the individual objects have no influence on each other.

8. Static code block to optimize the performance of the program. When the class is first loaded, each static block is executed in the order of the static block and is executed only once. ( executes only once when the class is loaded )

9. In Java, remember that static is not allowed to modify local variables. Do not ask why, this is the Java syntax of the provisions.

Ten. The string class in Java is a final class.

11. When a class is decorated with final, it indicates that the class cannot be inherited. Note that all member methods in the final class are implicitly specified as the final method.

12. For a final variable, if it is a variable of the base data type, its value cannot be changed once initialized, and if it is a variable of a reference type, it cannot be pointed to another object after it has been initialized.

13. A final modified reference variable cannot point to another object once it has been initialized, and the contents of the object that the reference variable points to are mutable.

14. In many cases it is easy to confuse the static with the final keyword, where static acts on the member variable to indicate that only one portion is saved, and the final function is used to ensure that the variable is immutable.

public class Test {public    static void Main (string[] args)  {        MyClass myClass1 = new MyClass ();        MyClass myClass2 = new MyClass ();        System.out.println (myclass1.i);        System.out.println (MYCLASS1.J);        System.out.println (myclass2.i);        System.out.println (MYCLASS2.J);     }} Class MyClass {Public    final double i = Math.random ();    public static Double J = Math.random ();} Running this code will reveal that the two J values are the same for each print, while the value of I is different. From here you can see the difference between final and static variables.

Java Basics:

Java Internal classes

Algorithm:

Sorting algorithm Summary

An improved string matching algorithm KMP

Final static Depth resolution

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.