Java Static and final

Source: Internet
Author: User

Java Improvement (vii)-----keyword staticstatic

There is no concept of global variables in Java, but we can use static to implement a "pseudo-global" concept, where static in Java means "global" or "static," which is used to modify member variables and member methods, and of course to decorate blocks of code.

Java divides memory into stack memory and heap memory, where stack memory is used to hold some basic types of variables, arrays, and references to objects, and heap memory mainly stores some objects. When the JVM loads a class, if the class has static decorated member variables and member methods, it creates a fixed-size area of memory for these member variables and member methods at a fixed location, with these "fixed" features, so that the JVM can access them very easily. At the same time, if the static member variables and member methods do not appear scoped, their handles will remain unchanged. At the same time static concept implies that it is not recoverable, that is, in that place, you modify, he will not be back to the original, you clean up, he will not come back.

member variables and member methods that are also modified by static are independent of the class, and do not depend on a particular instance variable, that is, it is shared by all instances of the class. References to all instances point to the same place, and any modification to it will result in changes to other instances.

 Public classUser {Private Static intUsernumber = 0 ;  PublicUser () {Usernumber++; }         Public Static voidMain (string[] args) {User user1=NewUser (); User User2=NewUser (); System.out.println ("User1 Usernumber:" +user.usernumber); System.out.println ("User2 usernumber:" +user.usernumber); }}    ------------output:user1 Usernumber:2user2 Usernumber:2

Static can be used to modify member variables and member methods, which we refer to as static variables and static methods, accessed directly through the class name.

ClassName.PropertyName

Classname.methodname (...)

A static code block is a block of code that, when the JVM loads the class, executes the code, which is very useful. (for the use of code blocks these days, please pay attention to)

2.1. Static variables

Static modified variables, which we call statically variable, do not use static modified variables called instance variables, the difference between them is:

A static variable is initialized as the class loads, it has only one in memory, and the JVM allocates memory only once, and all instances of the class share static variables that can be accessed directly through the class name.

However, the instance variable is different, it is accompanied by the instance, each creation of an instance will produce an instance variable, which is die with the instance.

So we generally use static variables in both cases: sharing data between objects, convenient access.

2.2. Static method

The static modification method we call it a statically method, which we call directly through the class name. Since he exists at the time of class loading, it does not depend on any instance, so the static method must be implemented, that is, he cannot be an abstract method.

The static method is a special method in a class, and we declare the method static only when we really need them. All methods, such as the math class, are statically static.

2.3. Static code block

The static code block, called the static block of code, is executed with the load of the class, and can be placed at will wherever it is.

Static does have many effects, but it also has some drawbacks.

1, it can only call the static variable.

2, it can only call the static method.

3, can not be quoted in any form this, super.

4. Static variables must be initialized when they are defined, and initialization time is earlier than non-static variables.

Java Improvement (15)-----keyword finalfinal

In programming, we may sometimes hope that some data cannot be changed, and that this is where the final comes into play. Final is the Java keyword, which means "this part cannot be modified". There are two reasons why you don't want to be changed: efficiency, design. There are three scenarios for final use: data, methods, classes.

Final data

Sometimes it is useful to have constant data, which can alleviate the burden of running the system. For these constant data I can be called "constants". The "constant" is mainly applied with the following two places:

1, the compiler period constant, can never be changed.

2, when the operation is initialized, we hope it will not be changed.

For a compile-time constant, it has already been initialized during the class loading process, so when the class is loaded it is immutable, and the compile time can be used in any computation that uses it, that is, the calculation can be performed at compile time. Of course, for compile-time constants, you can only use basic types, and you must initialize them when you define them.

Some variables, we want it to behave differently depending on the object, but at the same time we do not want it to be changed, we can use the run-time constant. For a run-time constant, it is both a basic data type and a reference data type. The base data type is immutable, and the reference data type is immutable, and the object content specified by the reference is mutable.

Final method

All methods that are final are not allowed to be inherited or changed, so the first reason to use the final method is to lock the method to prevent any subclasses from modifying it. As for the second reason is efficiency, I do not understand this efficiency problem very clearly, on the Internet excerpt this paragraph: in the early implementation of Java, if a method is indicated as final, it is agreed that the compiler will make all calls to the method into an inline call. When the compiler discovers a final method invocation command, it will execute the method invocation mechanism (pressing the parameters into the stack, jumping to the method code, and then jumping back and cleaning the parameters in the stack, processing the return value), according to its own discretion, skipping the normal invocation of the Insert program code. and replace the method call with a copy of the actual code in the method body. This will eliminate the overhead of the method call. Of course, if a method is large, your program code expands, so you may not see the performance improvement that comes with embedding, because the performance is reduced by the amount of time spent in the method.

The final method of the parent class is not covered by the quilt class, that is, the subclass is not able to exist the same way as the parent class.

Final class

If a class is modified with final, it indicates that the class is the final class, and it does not want or allow others to inherit it. For security or other reasons in the program design, we do not allow any changes to the class, and we do not want it to have subclasses, and at this point we can use final to decorate the class.

For a final decorated class, its member variables can be final or non-final. If it is defined as final, then the rules for final data are also appropriate for it. And its methods are automatically added to final, because the final class cannot be inherited, so this is the default.

Final parameter

In practice, we can modify a member variable, a member method, a class, or a parameter, if a parameter is final, in addition to the final modifier, which means the parameter is immutable.

If we modify this parameter in the method, the compiler will prompt you: The final local variable I cannot be assigned. It must is blank and not using a compound assignment.

Final and Static

Final and static use a magical chemical reaction when they are used together to modify member variables or to modify member methods.

For a member variable, the variable cannot be changed once it is assigned, we call it "global constant". can be accessed directly through the class name.

For member methods, it is not inheritable or changed. can be accessed directly through the class name.

Java Static and final

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.