Showing posts with label Access/Visibility Modifiers. Show all posts
Showing posts with label Access/Visibility Modifiers. Show all posts

Monday, 15 June 2015

Java modifiers for class members

  • static
  • final
  • abstract
  • synchronized
  • native
  • transient
  • volatile
1. static modifier

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
Example: The following example illustrates that the blank static final variable can be initialized in the static 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.

Monday, 8 June 2015

Java Other modifiers for interface members

Other modifiers for data members

Besides the visibility modifier public, we can also use modifiers static and final. Although we can use these modifiers but it is redundant as all data members are implicitly public, static and final.

Other modifiers for methods

Besides the visibility modifier public, we can also use modifier abstract. Although we can use abstract modifier but it is redundant as all methods are implicitly public, and abstract.

Java Other modifiers for top-level interfaces

(a) abstract

This is the only modifier other than visibility modifiers, which can be used with interface.

Interfaces just specify the method prototypes and not any implementation: they are, by their nature, implicitly abstract (i.e. they cannot be instantiated). We can declare an interface as abstract but it is redundant as interface is always abstract.

Java Other modifiers for top-level classes

Besides visibility modifiers, we can also use following modifiers before a top-class:

(a) abstract, and (b) final

(a) abstract

A class can be specified with the keyword abstract to indicate that it cannot be instantiated. A class containing abstract method must be declared as abstract otherwise it won’t compile. A class not containing any abstract method can also be declared abstract. Such a class can serve as a base class for a number of sub-classes.

(b) final

A class can be declared final to indicate that it cannot be extended. The final class marks the lower boundary of its implementation inheritance hierarchy. Only a class whose definition is complete can be declared final. A class cannot be both final and abstract at the same time.

Here are few important characteristics of the final class.

  • All the methods of a final class are also final i.e. they have the concrete implementation and cannot be over-ridden.
  • Some type checks become faster with final classes. In fact, many type checks become compile time checks and errors can be caught earlier. If the compiler encounters a reference to a final class, it knows that the object referred to is exactly of that type.

  • 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 cannot 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.

Java Member accessibility/visibility modifiers for interfaces

The only member accessibility/visibility modifier that can be used with data members and methods of an interface is public

The public is also the implicit accessibility/visibility modifier for interface members i.e. the members are always implicitly assumed to be public even if we do not use the modifier public.

Java Member accessibility/visibility modifiers for classes

By specifying member accessibility modifiers a class can control what information is accessible to clients (i.e. other classes). These modifiers help a class to define a contract so that clients know exactly what services are offered by the class.

Accessibility/visibility of members can be one of the following:

  • public
  • protected
  • default (also called package accessibility)
  • private

Note: Member accessibility modifier only has meaning if the class (or one of its subclasses) is accessible to the client. Also note that only one accessibility modifier can be specified for a member. The discussion applies to both instance and static members of classes.

Java Accessibility modifiers for Top-level classes and interfaces

  • public
  • default (package) accessibility

Saturday, 30 May 2015

Java Access/Visibility Modifiers

Access/visibility modifiers control access to a class, interface, data member or method. There are four levels of visibility in Java:


  • public
  • protected
  • package
  • private

1. Access/Visibility Modifiers for Top-Level Classes.

Visibility of a top-level class can be either public or package.

public

The keyword public is the only modifier, which can be used with a top-level class. If a class is to 
be visible to all the classes irrespective of their package, then it must be declared as public by specifying the modifier public, which should appear before the keyword class.

package

There is no keyword package for visibility control. In the absence of any access/visibility modifier before the top-level class, its visibility is only within the package in which it is defined. The concept of package is somewhat similar to the concept of friend classes in C++.

2. Access/Visibility Modifiers for Data Members and Methods.

When used in the variable or method declarations, access/visibility modifiers control access to the variable or method i.e. they decide who can access the variables and methods. These modifiers are not applicable for local variables as their visibility/scope is anyhow limited to the method/block in which they are declared.

public

Any method or variable is always visible in the class in which it is declared. The instance methods of a class can access any other method (instance as well as static method) or variable (instance as well as static variable) declared in the class. Similarly a static method of a class can access any other static method or static variable declared in the same class. 

If a method or variable is to be visible to all the classes, then it must be declared as public by specifying the modifier public, which should appear before the data type. For example, main() method is always declared as public. The reason is that main() method is accessed by the code which is part of JVM i.e. it is outside the class in which main() method is declared. Similarly an instance or static variable can be declared public by specifying modifier public as shown below:

public float width;
public static float width;

A variable or method declared as public has the widest possible visibility. It can be accessed from any class.

private

If you want that a method or variable should not be visible outside the class in which it is declared then its access modifier should be private. We use this modifier to hide the variable or method so that it cannot be accessed from outside the class. 

A variable declared as private has the least possible visibility but it is the most commonly used modifier to encourage data encapsulation and data hiding.  

package 

There is no keyword package. In the absence of any access/visibility modifier before a data member or method, its visibility is only within the package in which it is defined.

protected

A variable declared as protected can be accessed from all the classes belonging to the same package as that of the class in which member is declared. This visibility is similar to the package scope. But in addition to this a protected member can be accessed from any child class irrespective of the package in which it is declared.

Example: A simple class Box is defined below.

1 class Box
2 {
3    double width;
4    double height;
5    double length;
6 }

Example: The class Box is redefined here to include one method volume().

1 class Box
2 {
3    double width;
4    double height;
5    double length;
6    void volume()
7    {
8        double vol = width * height * length;
9        System.out.println(vol);
10  }
11 }