Why can't constructors be modified by abstract/static/FINAL/native/stricftp/synchronized and use of some keywords?

Source: Internet
Author: User

From: http://www.blogjava.net/realsmy/archive/2007/04/10/109256.html

From: http://www.wljcz.com/html/j2se/javajc/2010/0408/3262.html

From: http://www.javaeye.com/topic/72543

From: http://www.dingkao.com/a/100627/50164/

From: http://blog.pfan.cn/sword2008/13772.html

From: http://www.cnblogs.com/wjun530/archive/2007/09/15/893802.html

 

Unlike methods, constructors cannot be abstract, static, final, native, strictfp, or synchronized.
1. the constructor is not inherited, so it is not necessary to declare it as final.
2. Similarly, an abstract constructor will never be implemented. (So it cannot be declared as abstract)
3. the constructor is always associated with an object and called, so it makes no sense to declare it as static.
4. there is no actual need to define the constructor as synchronous, because it will lock the object during construction until all constructors complete their work, this construction process is usually inaccessible to other threads.(Synchronized)
5. The local method is very complicated, so JVM calling is very troublesome. You need to consider many cases. It is easier to implement JVM without the native keyword.

 

Note: usage of strictfp, native, transient, and volatile

1)Strictfp: Strictfp refers to FP-strict, that is, exact floating point.

When the strictfp keyword is not specified for floating-point operations on the Java Virtual Machine, the Java compiler and runtime environment perform these operations on the floating-point expressions in an action similar to the self-processing, as a result, you are often not satisfied with the results. Once strictfp is used to declare a class, interface or method, the declared scope of the Java compiler and runtime environment will be fully executed in accordance with the floating point specification IEEE-754. Therefore, if you want to make your floating point operations more accurate and do not use the strictfp keyword because the results of different hardware platforms are inconsistent.

You can declare a class, interface, and method as strictfp,Not AllowedDeclare the strictfp keyword for methods and constructors in the interface. For example:

I. Incorrect usage

Interface {
Strictfp void F (); // method in the interface
}
Public class fpdemo2 {
Strictfp fpdemo2 () {}// Constructor
}

2. Valid keyword strictfp

Strictfp interface a {}// Interface
Public strictfp class fpdemo1 {// class
Strictfp void F () {}// Method
}

2) Native: Native is the method modifier. The native method is a local method implemented by another language (such as C/C ++, Fortran, assembly. Sometimes a Java application needs to interact with an environment outside Java. This is the main reason for the existence of local methods.

Because methods are implemented externally, they do not need to be declared in Java code, which is somewhat similar to interface methods. Native can be used with some other modifiers,Abstract and interface methods cannot be modified using native..

When defining a native method, it does not provide an implementation body (for example, defining a Java Interface), because the implementation body is implemented outside of the non-Java language. Example:
Public class ihavenatives
{
Native public void native1 (int x );
Native static public long native2 ();
Native synchronized private float native3 (Object O );
Native void native4 (INT [] ary) throws exception;
}
The declaration of these methods describes how some non-Java code looks like in these Java code (view ).
The reason why abstract cannot be used: This is reasonable, because native implies that these methods have implementations, but these implementations are not Java, abstract: however, it is clear that these methods do not have an implementation body.

When native is connected to other Java identifiers, its meaning is the same as that of non-native method. For example, native static indicates that this method can beInstances that do not generate classesThis is very convenient. For example, if you want to use a native method to call a c class library. The third method above uses native synchronized.Before entering the implementation body of this method, the synchronization lock mechanism will be executed..

3) transient: Variable modifier (only field can be modified ).

Variables marked as transient are not persistent during object storage. When the object is serialized and stored in memory, some field data is not expected to be saved. To ensure security, you can declare these fields as transient.

4) Volatile: Variable modified by volatile. Every time a thread is accessed, the value of the member variable is forcibly re-read from the shared memory. In addition, when the member variables change, the thread is forced to write the change value back to the shared memory. In this way, two different threads always see the same value of a member variable at any time.

Differences between volatile and synchronized:

1. volatile essentially tells the JVM that the value of the current variable in the register (Working Memory) is uncertain and needs to be read from the main memory. Synchronized locks the current variable, only the current thread can access this variable, and other threads are blocked.
2. Volatile can only be used at the variable level; synchronized can be used at the variable, method, and class level.
3. Volatile can only realize variable modification visibility, but cannot guarantee atomicity. Synchronized can ensure variable modification visibility and atomicity.
4. Volatile will not cause thread blocking; synchronized may cause thread blocking.
5. Variables marked by volatile are not optimized by the compiler; variables marked by synchronized can be optimized by the compiler.

5) static:

1>Static Method:Generally, a method is defined as static in a class, that is, the method can be called using the class name without the need for the class object. Static methods are often used to provide some utilities for other classes in the application.

2>Static variables:Static variables are similar to static variables. All such instances share this static variable. That is to say, only one bucket is allocated during class loading, and all such objects can control this bucket. Of course, final is another option.

3>Static variables take precedence over any other non-static variables, regardless of the order in which they appear.

The code in static {} is used for explicit static variable initialization. This code is only initialized once, and when the class is loaded for the first time. When inheritance is involved, the static variables of the parent class will be initialized first, then the static variables of the Child class, and so on.

4> A common class cannot be declared as static. Only one internal class is allowed. In this case, the static internal class can be directly used as a common class without the need to instance an external class. However, it cannot be accessed from objects of static internal classes.Non-staticThe external class objects and methods (accessible to external static objects and methods ). For example:

Public class staticcls {
Public static void main (string [] ARGs ){
Outercls. innercls OI = new outercls. innercls ();
}
}
Class outercls {
Public static class innercls {
Innercls (){
System. Out. println ("innercls ");
}
}
}

PS: non-static internal class. Initialization Method:

Outerobject = new outerclass (constructor parameters );

Outerclass. innerclass innerobject=Outerobject. New innerclass(Constructor parameters );

Note that when creating a non-static internal class object, you must first create an external class object. As for the reason,Non-static internal class objects have references to their external class objects.An internal class object can access the content of its external class object, and even include private variables!

Differences between static and non-static internal classes: 1. The static internal class does not point to external references. 2. In any non-static internal class, there cannot be static data, static methods, or another static internal class (internal class nesting can be more than one layer ). However, static internal classes can have all of this.

6) Final

Final variable:

When defining a variable in a class, add the final keyword before it, that is, this variable cannot be changed once initialized, the unchangeable meaning here is that its value is immutable for the basic type, and its reference for the object variable cannot be changed.

It can be initialized in two places. One is its definition, that is, it is assigned a value directly when the final variable is defined, and the other is in the constructor. You can only choose one of these two places, either give a value at the time of definition, or give a value in the constructor, not both at the time of definition, in the constructor, another value is given.

Another usage is:The parameter in the definition method is final.For variables of the basic type, doing so does not have any practical significance, because variables of the basic type are passed values when calling methods, that is to say, you can change this parameter variable in the method without affecting the call statement. However, it is very practical for the object variable because the object variable is passed with its reference during transmission, in this way, your modification to the object variable in the method will also affect the object variable in the call statement,When you do not need to change the object variable used as a parameter in the method, using final to declare it will prevent you from accidentally modifying it and affecting the calling method..

Final method:
If you declare the method as final, it means that you already know that the functions provided by this method have met your requirements and do not need to be extended.This method cannot be overwritten by any class inherited from this class.But inheritance can still inherit this method, that is, it can be used directly. When the compiler calls the final method, it will transfer the embedded inline mechanism, which will make youWhen the final method is called, The method subject is directly inserted into the call.Instead of making routine method calls, such as saving breakpoints and pushing stacks, this may improve the efficiency of your program. However, when your method subject is very large, or if you call this method in multiple places, your calling subject code will expand rapidly, which may affect the efficiency.Use with cautionFinal defines the method.

Final class:
When you use final on a class, you need to consider it carefully, becauseThe final class cannot be inherited by anyone.That means this class is a leaf class in an inheritance tree, and this design has been considered perfect without modification or expansion.For members in the final class, you can define it as final or not final.WhileFor methods, because the class is final, it naturally becomes final.You can also explicitly add a final to the methods in the final class, but this is obviously meaningless.

7) What does static and final represent?
Static final is used to modify member variables and member methods. It can be simply understood as a "global constant "!
For variables, it means that once the value is given, it cannot be modified and can be accessed through the class name; for methods, it means it cannot be overwritten and can be accessed directly through the class name.

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.