Java abstract,static, final keyword usage __java

Source: Internet
Author: User
Tags modifier object object static class
Key concepts: Abstract classes, abstract methods, static classes, static internal classes, static methods, static variables, constants, global constants.
I. The use of abstract, modifier, you can modify class and Method 1, abstract modifier class, make this class an abstract class (detailed knowledge see Java abstract class and abstract method Bowen) This class will not be able to generate object instances, but can be declared as object variable type, that is, compile-time type , an abstract class is like a semi-finished product of a class, requiring subclasses to inherit and overwrite the abstract methods.
The 2,abstract modification method makes this method an abstract method, that is, only the declaration (definition) is not implemented, and the implementation part is replaced with ";". A subclass inheritance implementation (overwrite) is required.
3. Classes with abstract methods must be abstract classes. But abstract classes are not necessarily abstract methods, they can be all concrete methods. The abstract modifier must be placed in front of the class name when the class is decorated. The abstract modification method requires that its subclasses overwrite (implement) this method. A method that can invoke a subclass to overwrite (implement) in a polymorphic manner, which means that the abstract method must
4, subclasses, unless the subclass itself is an abstract class. A parent class is an abstract class, in which there are abstract methods, the subclass inherits the parent class, and all the abstract methods in the parent class are implemented (overwritten), the subclass has the ability to create an instance of the object, or the subclass must be an abstract class. An abstract class can have a constructor method, which is the constructor of the parent class (abstract class) that the subclass needs to invoke when it constructs the subclass object.
Two, static usage, the modified method is called the static method, the modified variable is called the static variable (see details static variable and static method's Bowen)
With regard to static variables and static method code blocks, that is, the JVM loads static variables and methods that are loaded with memory, as common variables and methods, that is, before the object is created, the corresponding variables and methods are created in memory.
1. static method public static void Prt (String s) {System.out.println (s); Typically, a method is defined as static in a class, which means that the method declared as static without the object of this class can be invoked with the following limitations: · They can invoke only other static methods. · They can only access static data. · They cannot refer to this or super in any way.
Calling a static method is the "class name. Method Name", and the use of static methods is simple as shown above. In general, static methods often provide utilities for other classes in the application, and a large number of static methods are defined for this purpose in Java's class libraries. Static methods are often used as tool classes or tool methods.
2 static variable int c = 0; 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. Static variables are similar to static methods. All such instances share this static variable, that is, when the class is loaded, only a single storage space is allocated, and all objects of this class can manipulate this block of storage space, of course, for final.
Static method code block: A method that can generally be initialized as an object.   static{String driverclass= "...";   String url= "";   String username= ""; String password= ""; The above code can initialize the database connection
3, static internal classes, usually a common class can not be set to static, only the internal class is set to static.        Class Outercls {public static class Innercls {Innercls () {System.out.println ("innercls"); Use static inner classes: public class Staticcls {public static void main (string[] args) {Outercls.innercls Oi = ne     W Outercls.innercls (); } }
4. The static and final piece is used to modify the member variable and the member method by means of what is static final, which can be easily understood as "global constant". 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.
5, Supplementary summary note: static means "global" or "static" meaning, used to modify the member variables and member methods, can also form a static code block, but the Java language does not have the concept of global variables.
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 you declare an object of its class, you do not generate a copy of the static variable, 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 by the class name, which is the class name. static method Name (parameter list ...) class name. Static variable Name
A code block decorated with static is a block of static code that is executed when the Java Virtual Machine (JVM) loads the class (very useful, hehe).
Static variables can be divided into two types according to whether they are static or not: One is a variable that is modified by static, a static variable or a class variable, and 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 an instance variable, no instance is created, and the instance variable is allocated once in memory, and the instance variable can have multiple copies in memory, with no effect (flexibility).
Static methods can be invoked directly through the class name, and any instance can be invoked, so the this and Super keywords cannot be used in static methods, and the instance variables and instance methods of the owning class are not directly accessible (that is, the member variable and member method without static). Only static member variables and member methods of the owning class can be accessed. Because instance members are associated with a particular object.
Because the static method is independent of any instance, the static method must be implemented, not abstract.
Iii. Use of final keywords
1. The general name of the modifier base data member is called a constant, and the value must be set at the time of declaration. Also become a compile-time constant. This is the main purpose of final, which is equivalent to C + + 's const, that is, the member is decorated as a constant, meaning that it cannot be modified. For example, PI and E in the Java.lang.Math class are final members with values of 3.141592653589793 and 2.718281828459045.
2. The final that modifies the reference of a class or object is generally less used.  In Java, we cannot allow an object to be decorated as final, but only to modify the object's reference, which means that even if you write public final A = new A (); In fact, the data of the object that a is pointing to can still be modified and cannot be modified by the reference value of a itself, that is, you can no longer assign a value to a. The same situation appears in the array, such as public final int[] A = {1, 2, 3, 4, 5}, in fact, the values in a are modifiable, that is, you can write a[0] = 3. As far as we know, the data in the Java array cannot be modified to be unmodified, and C + + can.
3. Subclasses of a class that modifies a method can only be used without modification or as overridden. The const of the decorated member object in final and C + + of the cosmetic method is very different. First, the final meaning of the cosmetic method is not "not modifiable," but rather that the method cannot be redefined by the inherited member. (Note that what is said here cannot be redefined, not that subclasses must not define a method of the same name. If the method of the parent class is a private type, and the subclass is allowed to define the method, this cannot be redefined to mean that the polymorphism of the method overrides cannot be achieved by rewriting the method, such as not wishing a A = new B (); A.F (); Such an overriding method occurs)
In addition, when a method is modified to the final method, the means that the compiler might load the method inline (inline), which means that the compiler does not invoke the method in the same way as the normal calling function, but instead simply copy the code within the method to the original code by a certain modification ( Inserts a method body directly into the call, rather than making a method call. This allows the code to execute faster (because the overhead of invoking the function is omitted), such as calling Arr.length () in int[] arr = new Int[3]. On the other hand, private methods are implicitly modified by the compiler to final, which means that private final void F () and private void f () are not different.
4. A final class that modifies a class cannot be inherited, such as a string class. When a class is decorated to final, it has a clear meaning that the class is not allowed to be inherited, that is, the class is "had out", and any operation that inherits it will end in a compilation error. This also underscores the reason why Java uses final instead of const as an identifier. (the member variable may not be final, the member method is directly final)
There is more in-depth knowledge about final, here do not do too deep understanding.
The Java language keyword synchronized usage, mainly used in multiple threads. When it is used to modify a method or a block of code, it is possible to ensure that at most one thread at a time executes that segment of code.
1. When two concurrent threads access the synchronized (this) synchronized code block in the same object, only one thread can be executed at a time. Another thread must wait until the current thread finishes executing the code block before executing the code block.
2. However, when a thread accesses a synchronized (this) synchronization code block of object, another thread can still access a non-synchronized (this) synchronized code block in that object.
3, especially crucially, when a thread accesses a synchronized (this) synchronized code block of object, access to all other synchronized (this) synchronized blocks of the object is blocked by other threads.
4. The third example also applies to other synchronized code blocks. That is, when a thread accesses a synchronized (this) synchronization code block of object, it obtains the object lock of the objects. As a result, access to all of the synchronization code portions of the object object by other threads is temporarily blocked. 5, the above rules for other object locks also apply.
Summing up: The scope of the Synchronized keyword is two kinds: 1 is a synchronized method within an object instance where synchronized Amethod () {} can prevent multiple threads from simultaneously accessing the object (If an object has multiple synchronized methods, as long as one thread accesses one of the synchronized methods, other threads cannot access any of the synchronized methods in the object at the same time). At this point, the synchronized method of different object instances is not interfered. That is, other threads can still access the Synchronized method in another object instance of the same class at the same time;
2 is the scope of a class, synchronized static astaticmethod{} prevents multiple threads from accessing the synchronized static method in this class at the same time. It can work on all object instances of a class. 2, in addition to the method before using the Synchronized keyword, the synchronized keyword can also be used in a method of a block, indicating that only the resources of the block mutex access. The usage is: synchronized (this) {/* block/}, its scope is the current object;
3, the Synchronized keyword is not inherited, that is, the base class method synchronized F () {} is not automatically synchronized F () {} in the inheriting class, but instead becomes F () {}. An inheriting class requires that you explicitly specify one of its methods as the Synchronized method;
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.