abstract
This applies to classes and methods.
A class must be declared abstract if any of the following conditions is true :
- The class has any abstract methods.
- The class inherits any abstract methods but does not implement them.
- The class declares that it implements an interface but does not implement all of its methods.
In a way, abstract is the opposite to final. A final class cannot be sub-classed but an abstract class must be sub-classed. An abstract class can have non-abstract methods.
When applied to a method, it means that it has not been implemented in its class. Any other class extending this class must either implement the inherited abstract method or itself be declared abstract.
final
This applies to classes, methods and variables.
A variable can be declared as final. Doing so prevents its contents being changed. This means a final variable must be initialized when it is declared. It is common coding convention to choose all uppercase identifiers for ‘final’ variables. Final variables do not occupy memory on a per-instance basis. Thus, it is essentially a constant. If a final variable is a reference to an object, it is the reference that must stay the same, not the object. It means that the reference cannot be assigned to some other object, but data of the object can be changed.
A final class cannot be sub-classed.
A final method cannot be overridden.
static
This can be applied to variables, methods and initializer blocks.
When applied to a variable, the variable belongs to the class itself and not to its objects. All the objects of the class share the variable. If you modify the value of a static variable in one object, the value gets changed for all the objects of the class since there is only one variable being shared among all the objects.
A static initializer block is executed when the class is loaded.
Methods declared static have several restrictions:
- They can only call other static methods.
- They must only access static data.
- They cannot refer to ‘this’ or ‘super’ in anyway.
- A static method cannot be overridden to be non static.
native
It can be applied to methods only.
It indicates that the method body is to be found elsewhere i.e. outside the JVM, in a library. Native code is written in a non java language and compiled for a single target machine type.
transient
It applies only to variables. A transient variable is not stored as part of its object’s persistent state. Many objects, especially those implementing Serializable or Externalizable interfaces, can have their states serialized and written to some destination outside the JVM. This is done by passing the object to the writeObject() method of the ObjectOutputStream class. If the stream is chained to a file output stream, then the object’s state is written to a file. If the stream is chained to a socket’s output stream then the object’s state is written to the network. In both cases, the object can be reconstituted by reading from an object input stream.
There will be times when an object will contain extremely sensitive data. Once an object is written to a destination outside JVM, none of the Java’s elaborate security mechanisms is in effect. If you declare a variable transient, it’s value will not be written out during serialization.
volatile
It is applied only to variables. It indicates that such variables might be modified asynchronously, so the compiler takes special precautions. Volatile variables are of interest in multi-processor environments.
public
A class, method or variable declared public can be accessed by any other code in a program.
private
This member can only be accessed by other members of its class.
default
when a member does not have an explicit access specification, it is visible to subclass as well as to other classes in the same package.
protected
This allows access from everywhere but except for different package non-subclass.
Note: You cannot combine some of these modifiers together. Some of the cases are:
- No two access modifiers can be combined. Such as public private, protected public or private protected.
- abstract and final.
- Native methods cannot be abstract, or strictfp.
- An abstract method cannot be static, final, synchronized, native, private, or strictfp.
No comments:
Post a Comment