Modifiers

Modifiers are Java keywords that are used to declareattached to a particular instance of a class but rather
features in applications. They affect either the lifetimebelong to a class as a whole. They are declared using
or the accessibility of a feature. A feature may be athe following syntax:static type varIdentifier;where, the
class, a method, or a variable.name of the variable is varIdentifier and its data type is
Modifiers that affect the visibility of a feature are calledtype.
access modifiers or visibility modifiers. The remainingStatic variables that are not explicitly initialized in the
modifiers do not fall into any clear categorization andcode are automatically initialized with a default value.
may be called storage and lifetime modifiers.The default value depends on the data type of the
The Access Modifiersvariables.
Access modifiers control the access of a feature.Methods declared with the keyword static as modifier
They enable a programmer to determine whetherare called static methods or class methods. They are
access to a feature is limited to a particular class, aso called because they affect a class as a whole and
class and its subclass, or a package, or if it isnot a particular instance of the class. Static methods
accessible without any restriction. Following are theare always invoked without reference to a particular
features of a class:instance of a class.
The class itselfA static method suffers from the following restrictions:
Its class variableA static method can only call other static methods.
Its methods and constructorsA static method must only access static data.
Java provides three access modifiers: public, protected,A static method cannot reference to the current
and private. If none of the three access modifiers isobject using keywords super or this.
used to declare a feature, Java provides a fourthIf a method is not declared as static, it is called an
modifier for which there is no name. It may be called ainstance method. Instance methods are associated
default access, a package access, or a friendlywith objects and not with classes. Furthermore, they
access modifier. A feature may be declared using atcan access all the variables in a class whether they
most one access modifier. A compile-time error willare instance fields or class fields.
occur if more than one access modifier is used toNon-static variables cannot be accessed from within a
declare a feature. The access modifiers are brieflystatic method. Therefore, the following code will not
described as follows:privatecompile successfully:public class ClsStat{int x;public
The private modifier is the most restrictive accessstatic void main(String argv[]){
modifier. It limits the visibility of methods and instanceSystem.out.println(x); //a compile-time error
variables to the class in which they are defined. An}
instance variable declared as private is visible (or}
accessible) only by methods within the same class, butIn the given code a compile-time error will occur. As
any other classes or objects cannot access it. Athe main method is static, it cannot access the
method declared as private can be invoked bynon-static field (i.e., x) in the ClsStat class.
methods in that same class but not by any otherThe static modifier can be used to declare a nested
classes. In addition, neither private variables nor privateclass. A nested class declared as static is called a
methods are inherited by subclasses.static nested class. A static nested class can directly
A) default accessaccess only static variables and methods defined in its
When none of the three access modifiers is used toenclosing class. However, it cannot refer directly to
declare a feature, the default access modifier isinstance variables or methods defined in its enclosing
provided by Java. This modifier enables a feature toclass. It can refer them only through an object
be accessed by any other classes, provided that thereference.
classes are in the same package in which the classA static initializer is a block of code, enclosed within
that defines them belongs to. Variables or methodscurly brackets and labeled as static. It is not a part of a
declared without any access modifiers are accessiblemethod. A class may have one or more static
to all the other classes in the same package in whichinitializers. All the static initializers in a class are
the class containing them belongs to.executed exactly once and in the same order in which
B) protectedthey appear in the class. The execution of static
An intermediate level of access between packageinitializers takes place at the class load time.final
and private is protected. Methods or variables declaredThe final modifier can be used to declare classes,
as protected are accessible to all classes within themethods, and variables.
same package in which the class that defines themA class can be declared as final if its definition is
belongs to. They are also accessible to subclasses ofcomplete and no subclasses are needed. The use of
the class that have been defined outside the package.the final keyword in a class declaration suggests that
C) publicthe class cannot be subclassed. In other words, a final
The most unrestrictive access modifier is public. It canclass cannot have any subclasses. For example, the
be used to declare a class, a method, or a variable. Afollowing class declaration will result in a compile-time
class declared as public is accessible in any programerror:
without any restriction. It also enables a method, aClass MyClass extends Math{
constructor, or a variable to be accessed anywhere in//other code
a program where its class can be accessed.}
Access modifiers are generally used to declare classAs the java.lang.Math class is final, its name cannot
level variables. As variables declared within a methodappear in the extends clause of another class
can only be used within its enclosing method, they maydeclaration. Declaring a class as final implicitly makes all
not have access modifiers.the methods in the class as final. Therefore, methods
Other modifiersof a final class are never overridden
1) abstractWhen a method is declared as final, it cannot be
The abstract modifier can be used to declare classesoverridden or hidden by a subclass. In other words, a
and methods. A class declared as abstract is asubclass cannot introduce a new version of the
partially implemented class. A class is declared asmethod. Methods declared as private and all the
abstract when it contains one or more abstractmethods declared in a final class are implicitly final, and
methods. A class is said to contain abstract methodsthere is no need to explicitly declare them as final.
in any of the following conditions:Variables declared with the keyword final as a
The body of the class explicitly declares an abstractmodifier are called final variables. They are so called
method.because once initialized a value, their values cannot be
The class inherits one or more abstract methods frommodified. Any attempt made to modify the value of a
its abstract superclass for which it does not providepre-initialized final variable will result in a compile-time
implementations.error.
The class directly inherits an interface that contains anA final variable is declared as shown below:final double
abstract method but the class neither declares theIAMFINAL=0;where, IAMFINAL is the name of variable
method nor does it inherit a method that implements it.whose data type is double. This statement declares
If a class containing an abstract method is notthe variable IAMFINAL as final and initializes it with the
declared as abstract, a compile-time error will occur. Avalue 0 at the same time. Further attempts to change
class can also be declared as abstract even if it hasthe value of IAMFINAL will result in a compile-time
no abstract methods.error.native
As the implementation of an abstract class isMethods implemented in a language other than Java
incomplete, it is not possible to create an instance ofare declared using the native keyword as modifier.
an abstract class. However, subclasses of an abstractThese methods are declared by using the native
class that itself is not declared as abstract can bekeyword. The body of a native method is replaced by
instantiated. Subclasses that extend an abstract classa semicolon, which indicates that the implementation of
are responsible for implementing the unimplementedthe method is omitted. When a method is declared as
part of the abstract class. A class declared asnative, the JVM arranges for a native method call to
abstract may also contain non-abstract methods.result in execution passing to native library
An abstract method is a method withoutcode.synchronized
implementation, i.e., an abstract is not defined in theThe use of the synchronized keyword in a method
class in which it is declared. The following points mustdeclaration ensures that at a time only one thread can
be noted about the declaration of an abstract method:execute the block of statements following the method
An abstract method declaration provides the signaturedeclaration. Other threads wanting access to the
of the method, return type, and throws clause (if any),method are forced to wait until the currently executing
but it does not provide an implementation of thethread returns from the method.transient
method. The declaration of an abstract method endsOnly variables can be declared as transient. When
with a semicolon rather than a block of code.used as a modifier in a variable declaration, it suggests
The class in which the abstract method is declaredthat a variable may not be written out when the class
must itself be declared as abstract.is serialized.volatile
The use of the abstract keyword in a methodThe keyword volatile can be used to declare variables.
declaration along with the modifiers: final, static, native,The use of the keyword volatile in a variable
synchronized, or private is not permitted.declaration suggests the compiler that multiple threads
By default, a method is non-abstract and it requires amay access the variable. Therefore, the value of the
block of code after the declaration part.variable may change unexpectedly. A compile-time
2) staticerror will occur on declaring a variable both volatile and
The static modifier can be used to declare an innerfinal.
class, a method, and a variable. Static means one perOrder of appearance of modifiers:
class, not one for each object no matter how manyIf two or more modifiers appear in the declaration of a
instances of the class might exist. This means thatfeature, they both affect the functionality of the
static features can be used without creating anfeature. The order of appearance has no effect on
instance of a class.the functionality of the feature. For example, public
Variables declared by using the static keyword as astatic means the same as static public.
modifier are called static variables. They are not