Monday 27 January 2014

               Some Important Modifiers in Java


Introduction
In this article we are going to describe some important modifiers which are used with method data members etc. We also describe separate examples of each modifier. In Java there are many modifiers used for its various purposes. The following list gives all the modifiers. In the following table some of the modifiers are used very commonly in all languages such as public, protected, static, volatile etc. And most of you know about the use of these common modifiers. So now we describe only three of the modifiers; native, transient and volatile.
Java Modifier Name and Description Table
Modifier NameUsed on Description
publicclass

interface

member


Accessible anywhere


 
privatememberAccessibilty only in its class (which defines it).
protectedmemberAccessibility only within its package and its subclasses
none
 
class

interface

member
 
Accessible only in its package
finalclass

method

field

variable
Cannot be subclassed

Cannot be overridden and dynamically looked up

Cannot change its value. static final fields are compile-time constants.

Cannot change its value.
abstractclass

interface

method
Contains unimplemented methods and cannot be instantiated.

All interfaces are abstract. Optional in declarations

No body, only signature. The enclosing class is abstract
staticclass

method

field


initializer
Make an inner class a top-level class

A class method, invoked through the class name.

A class field, invoked through the class name 
one instance, regardless of class instances created.

Run when the class is loaded, rather than when an instance is created.
strictfpclass

method

field


initializer
All methods in the class are implicitly strictfp.

All floating-point computation done is strictly conforms to
the IEEE 754 standard. All values including intermediate results
must be expressed as IEEE float or double values. 
It is rarely used.
synchronizedmethodFor a static method, a lock for the class is acquired before
executing the method. For a non-static method, a lock for the specific
object instance is acquired.
volatilefieldAccessible by unsynchronized threads, very rarely used.
transientfieldNot be serialized with the object, used with object serializations.
nativememberAccessible only in its class(which defins it).

No comments:

Post a Comment