- static
- final
- abstract
- synchronized
- native
- transient
- volatile
The static members belong to the class in which they are declared, and are not part of any instance of the class. Depending on the accessibility modifiers of the static members in a class, client can access these by using the class name or through object references of the class.
static variables (also called class variables)
These variables only exist in the class they are defined in. When the class is loaded, static variables are initialized to their default values, if no explicit initialization expression in specified.
The static member variables are often used when tracking global information about the instances of a class.
static methods
These are also known as class methods. A static method in a class can directly access other static members in the class. It cannot access instance (i.e. non-static) members of the class, as there is no notion of an object associated with a static method.
However, note that a static method in a class can always use a reference of the class’s type to access its members regardless of whether these members are static or not.
A typical static method might perform some task on behalf of the whole class and/or objects of the class.
An instance in a subclass cannot override a static method in the superclass. The compiler will flag this with an error (means a static method in super class cannot be overridden by non static methods in subclass). A static method is class specific and not part of any object, while overriding, methods are invoked on the behalf of objects of the subclass. However, a static
method in a subclass can hide a static method in the superclass.
Methods declared as static have several restrictions:
- They can only call other static methods.
- They can only access static data members
- They cannot refer to this or super in anyway
static initialization block
A java class can have one or more static initialization block. The syntax of the static initialization block is as follows:
static
{
code
}
A static initialization block can be thought of as a static method, which is invoked implicitly/automatically as soon as the class is loaded.
The static block can be useful in the following situations:
- Dynamic initialization of static variables.
- For performing one time activity at the time of loading class. For example, loading jdbc driver class.
- Loading small database/configuration files in Hashtable, HashMap, Properties etc.
2. final modifier
The modifier final can be used with
- local variables
- instance variables/data members
- static variables/data members
- instance methods
2.1 final variables
A final variable is normally initialized at the time of declaration and its value cannot be modified after assigning once. Here are few important points related to final variables:
- A final variable is a constant, despite being called a variable. Its value cannot be changed once it has been initialized. This applies to instance, static and local variables, including parameters that are declared final.
- The final is the only modifier applicable to local variables or formal parameters.
- A final variable of a primitive data type cannot change its value once it has been initialized.
- A final variable of a reference type cannot change its reference value once it has been initialized, but the state of the object it denotes can still be changed.
- Normally a final variable is initialized at the time of declaration but it is not must. Such a final variable is also called blank final variable, and must be initialized once before it is being used.
- The final static variables are commonly used to define named constants, for example Integer.MAX_VALUE, which is the maximum int value.
2.2 local final variables
Example: The following example illustrates that local final variable can be left blank as discussed above but must be initialized before first use. The variable x is initialized just before displaying its value on the monitor.
class FinalTest //makes use of blank final
{ public static void main(String args[])
{ final int x;//initialization at declaration time not must
x = 50;
System.out.println(x);
}
}
Output:
50
The following program will not compile as final variable is initialized after first use.
class FinalTest
{ public static void main(String args[])
{ final int x; //blank final
System.out.println(x);
x = 50;
}
}
The following program will not compile as final variable is initialized twice.
class FinalTest
{ public static void main(String args[])
{ final int x;
x = 50;
System.out.println(x);
x = 60;
}
}
2.3 instance final variables
An instance final variable can be left blank but must be initialized before obtaining a reference of an object of the class containing final variable. So if we do not initialize at the time of declaration then the other places at which final variables can be initialized are:
- Constructor
- Initialization block
Example: The following program will not compile, as final variable is not initialized. The concept of initialization by default value is not applicable to final variables as they cannot be modified after initialization.
class FinalTest
{ final int x;//No default initialization for final instance variables
}
class MyClass
{
public static void main(String args[])
{ FinalTest f=new FinalTest();
}
}
Output: (Compile time error): variable x might not have been initialized
Example: The following program will also not compile, as final variable is initialized after obtaining a reference.
class FinalTest
{ final int x;
}
class MyClass
{ public static void main(String args[])
{ FinalTest f=new FinalTest();
System.out.println(f.x);
f.x = 10;
System.out.println(f.x);
}
}
Example: The following example illustrates that a blank final instance variable can be initialized in the constructor.
class FinalTest
{ final int x;
public static void main(String args[])
{ FinalTest f = new FinalTest();
}
FinalTest()
{ x = 10;
}
}
Output: 10
System.out.println(f.x);
Example: The following program illustrates that a blank final variable can be initialized with a dynamic value and can be referred with this keyword. So different objects of the same class can have different values of the final variable.
class FinalTest
{ final int x;
public static void main(String args[])
{ FinalTest f = new FinalTest(20);
}
FinalTest(int x)
{ this.x = x;
}
}
Output: 20
System.out.println(f.x);
Example: The following program illustrates that a final instance variable can be initialized in a initialization block.
class FinalTest
{ final int x;
public static void main(String args[])
{ FinalTest f = new FinalTest();
}
{
}
}
Output: 30
Initialization block
A java class can have one or more initialization blocks. The syntax of the initialization block is as follows:
{
code
}
An initialization block can be thought of as a constructor, which is invoked
implicitly/automatically as soon as the object is created. In fact if a class contains initialization
System.out.println(f.x);
x = 30;
blocks then they are executed in the order of definition from top to bottom before any constructor.
The initialization block can be useful in the following situations:
- Dynamic initialization of instance variables.
- For defining code which is common to all constructors. This will increase the code reusability and reduce maintenance.
2.4 static final variables
A static final variable can be left blank but must be initialized before class is available to the program. So if we do not initialize at the time of declaration then the only place at which static final variable can be initialized is:
- static Initialization block
class FinalTest
{ static final int x;
public static void main(String args[])
{
}
static
{
}
}
Output:
40
System.out.println(FinalTest.x);
x = 40;
2.7.2.5 final methods
- A final method in a class is complete (i.e. has an implementation) and can not be overridden in any subclass. Subclasses are thus restricted in changing the behavior of the method.
- The compiler is able to perform certain code optimizations for final methods, because certain assumptions can be made about such members. When a non-final method is invoked, the run time system determines the actual class of the object, binds the method invocation to the correct implementation of the method for that type, and then invokes the implementation. In case of a final method we are sure that sub-class can not override the method so the binding is done at the compile time as in case of private and static methods, which would definitely improve the performance.
- In case of a final method the compiler may replace an invocation with the actual body of the method like Macro. This mechanism is known as ‘inlining’. In C++, we have the option of declaring a function as inline (although final decision is taken by the compiler) but in Java, the compiler takes the decision.
3. abstract modifier
The abstract modifier can be used only with instance methods. An abstract method does not have an implementation, that is, no method body is defined for an abstract method, only the method prototype is provided in the class definition. Its class is also abstract (i.e., incomplete) and must be explicitly declared as abstract. Subclasses of an abstract class must then provide the method implementation; otherwise, they are also abstract.
Here are some important points related to abstract methods:
- An abstract method cannot be declared private. This is obvious as we can not override a private method, while in case of an abstract method body is always provided in the sub-class by over-riding the abstract method.
- Only a non-static/instance method can be declared abstract since static methods cannot be overridden, declaring an abstract static method would make no sense and will result in compilation error.
- Only an instance method can be declared abstract since static methods cannot be overridden, declaring an abstract static method would make no sense and will result in compilation error.
- A final method cannot be abstract and vice-versa.
- The keyword abstract cannot be combined with any non-accessibility modifiers for methods.
- Methods specified in an interface are implicitly abstract, as only the method prototypes are defined in an interface.
4. synchronized modifier
The synchronized keyword can be used with methods and code blocks.
Several threads can execute simultaneously in a program. They might try to execute several methods on the same object simultaneously. If it is desired that only one thread at a time can execute a method in the object, the methods can be declared synchronized. Their execution is then mutually exclusive among all threads. At any given time, at most one thread can be executing a synchronized method on an object. This discussion also applies to static synchronized methods of a class. The detailed discussion is available in the chapter on multi-threading.
Example:
Suppose you have two threads that are responsible for updating a bank balance, called threads A and B. Suppose also that the account has $100 in it. Now simultaneously, thread A tries to deposit $50 in the account, while thread B tries to deposit $75. Both threads proceed to query the account balance. They both find it to be $100. Each of them independently, adds its deposit to the old sum and sets the new balance accordingly. If thread A finishes last, the account contains
$150. If thread B finishes last, the account balance is $175. Of course, neither of these new figures is correct. The account should have $225 in it (100+50+75). The problem is that both threads tried to execute the same code (querying and changing the balance) at the same time.
This is exactly the scenario that the synchronized keyword can prevent.
The synchronized modifier does not apply to classes or member variables.
5. native modifier
The native modifier can refer only to methods. Like the abstract keyword, native indicates that the body of a method is to be found elsewhere. The body of a native method is entirely outside the JVM, in a library.
Native code is written in a non-java language, typically C or C++, and compiled for a single target machine type. Thus Java’s platform independence is violated.
People who port Java to new platforms implement extensive native code to support GUI components, network communication, and a broad range of other platform-specific functionality.
However, it is rare for the application and applet programmers to need to write native code.
6. transient modifier
Objects can be stored using serialization. Serialization transforms objects into an output format that is conducive for storing objects. Objects can later be retrieved in the same state as when they were serialized, meaning that all fields included in the serialization will have the same values as at the time of serialization. Such objects are said to be persistent. A field can be specified as
transient in the class declaration, indicating that its value should not be saved when objects of the class are written to persistent storage.
Example:
class Experiment implements Serializable
{ transient int currentTemperature; //transient
double mass; //persistent value
}
Specifying the transient modifier for static variables is redundant and therefore, discouraged. The static variables are not part of the persistent state of a serialized object.
7. volatile modifier
During execution, compiled code might cache the values of fields for efficiency reasons. Since multiple threads can access the same field, it is vital that caching is not allowed to cause inconsistencies when reading and writing the value in the field.
The volatile modifier can be used to inform the compiler that it should not attempt to perform optimization on the field, which could cause unpredictable results when the field is accessed by multiple threads.
No comments:
Post a Comment