save up to 40%

Modifiers

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

Modifiers

Rating:

Modifiers are Java keywords that are used to declare features in applications. They affect either the lifetime or the accessibility of a feature. A feature may be a class, a method, or a variable.

Modifiers that affect the visibility of a feature are called access modifiers or visibility modifiers. The remaining modifiers do not fall into any clear categorization and may be called storage and lifetime modifiers.

The Access Modifiers

Access modifiers control the access of a feature. They enable a programmer to determine whether access to a feature is limited to a particular class, a class and its subclass, or a package, or if it is accessible without any restriction. Following are the features of a class:

  • The class itself

  • Its class variable

  • Its methods and constructors
Java provides three access modifiers: public, protected, and private. If none of the three access modifiers is used to declare a feature, Java provides a fourth modifier for which there is no name. It may be called a default access, a package access, or a friendly access modifier. A feature may be declared using at most one access modifier. A compile-time error will occur if more than one access modifier is used to declare a feature. The access modifiers are briefly described as follows:

  • private

    The private modifier is the most restrictive access modifier. It limits the visibility of methods and instance 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, but any other classes or objects cannot access it. A method declared as private can be invoked by methods in that same class but not by any other classes. In addition, neither private variables nor private methods are inherited by subclasses.


  • default access

    When none of the three access modifiers is used to declare a feature, the default access modifier is provided by Java. This modifier enables a feature to be accessed by any other classes, provided that the classes are in the same package in which the class that defines them belongs to. Variables or methods declared without any access modifiers are accessible to all the other classes in the same package in which the class containing them belongs to.


  • protected

    An intermediate level of access between package and private is protected. Methods or variables declared as protected are accessible to all classes within the same package in which the class that defines them belongs to. They are also accessible to subclasses of the class that have been defined outside the package.


  • public

    The most unrestrictive access modifier is public. It can be used to declare a class, a method, or a variable. A class declared as public is accessible in any program without any restriction. It also enables a method, a constructor, or a variable to be accessed anywhere in a program where its class can be accessed.
Access modifiers are generally used to declare class level variables. As variables declared within a method can only be used within its enclosing method, they may not have access modifiers.

Other modifiers

abstract

The abstract modifier can be used to declare classes and methods. A class declared as abstract is a partially implemented class. A class is declared as abstract when it contains one or more abstract methods. A class is said to contain abstract methods in any of the following conditions:

  • The body of the class explicitly declares an abstract method.

  • The class inherits one or more abstract methods from its abstract superclass for which it does not provide implementations.

  • The class directly inherits an interface that contains an abstract method but the class neither declares the method nor does it inherit a method that implements it.
If a class containing an abstract method is not declared as abstract, a compile-time error will occur. A class can also be declared as abstract even if it has no abstract methods.

As the implementation of an abstract class is incomplete, it is not possible to create an instance of an abstract class. However, subclasses of an abstract class that itself is not declared as abstract can be instantiated. Subclasses that extend an abstract class are responsible for implementing the unimplemented part of the abstract class. A class declared as abstract may also contain non-abstract methods.

An abstract method is a method without implementation, i.e., an abstract is not defined in the class in which it is declared. The following points must be noted about the declaration of an abstract method:

  • An abstract method declaration provides the signature of the method, return type, and throws clause (if any), but it does not provide an implementation of the method. The declaration of an abstract method ends with a semicolon rather than a block of code.

  • The class in which the abstract method is declared must itself be declared as abstract.

  • The use of the abstract keyword in a method declaration along with the modifiers: final, static, native, synchronized, or private is not permitted.
By default, a method is non-abstract and it requires a block of code after the declaration part.

static

The static modifier can be used to declare an inner class, a method, and a variable. Static means one per class, not one for each object no matter how many instances of the class might exist. This means that static features can be used without creating an instance of a class.

Variables declared by using the static keyword as a modifier are called static variables. They are not attached to a particular instance of a class but rather belong to a class as a whole. They are declared using the following syntax:

static type varIdentifier;

where, the name of the variable is varIdentifier and its data type is type.

Static variables that are not explicitly initialized in the code are automatically initialized with a default value. The default value depends on the data type of the variables.

Methods declared with the keyword static as modifier are called static methods or class methods. They are so called because they affect a class as a whole and not a particular instance of the class. Static methods are always invoked without reference to a particular instance of a class.

A static method suffers from the following restrictions:

  1. A static method can only call other static methods.

  2. A static method must only access static data.

  3. A static method cannot reference to the current object using keywords super or this.
If a method is not declared as static, it is called an instance method. Instance methods are associated with objects and not with classes. Furthermore, they can access all the variables in a class whether they are instance fields or class fields.

Non-static variables cannot be accessed from within a static method. Therefore, the following code will not compile successfully:

  1. public class ClsStat{

  2.    int x;

  3.    public static void main(String argv[]){

  4.        System.out.println(x);        //a compile-time error

  5.    }

  6. }
In the given code a compile-time error will occur. As the main method is static, it cannot access the non-static field (i.e., x) in the ClsStat class.

The static modifier can be used to declare a nested class. A nested class declared as static is called a static nested class. A static nested class can directly access only static variables and methods defined in its enclosing class. However, it cannot refer directly to instance variables or methods defined in its enclosing class. It can refer them only through an object reference.

A static initializer is a block of code, enclosed within curly brackets and labeled as static. It is not a part of a method. A class may have one or more static initializers. All the static initializers in a class are executed exactly once and in the same order in which they appear in the class. The execution of static initializers takes place at the class load time.

final

The final modifier can be used to declare classes, methods, and variables.

A class can be declared as final if its definition is complete and no subclasses are needed. The use of the final keyword in a class declaration suggests that the class cannot be subclassed. In other words, a final class cannot have any subclasses. For example, the following class declaration will result in a compile-time error:

Class MyClass extends Math{
//other code
}


As the java.lang.Math class is final, its name cannot appear in the extends clause of another class declaration. Declaring a class as final implicitly makes all the methods in the class as final. Therefore, methods of a final class are never overridden

When a method is declared as final, it cannot be overridden or hidden by a subclass. In other words, a subclass cannot introduce a new version of the method. Methods declared as private and all the methods declared in a final class are implicitly final, and there is no need to explicitly declare them as final.

Variables declared with the keyword final as a modifier are called final variables. They are so called because once initialized a value, their values cannot be modified. Any attempt made to modify the value of a pre-initialized final variable will result in a compile-time error.

A final variable is declared as shown below:

final double IAMFINAL=0;

where, IAMFINAL is the name of variable whose data type is double. This statement declares the variable IAMFINAL as final and initializes it with the value 0 at the same time. Further attempts to change the value of IAMFINAL will result in a compile-time error.

native

Methods implemented in a language other than Java are declared using the native keyword as modifier. These methods are declared by using the native keyword. The body of a native method is replaced by a semicolon, which indicates that the implementation of the method is omitted. When a method is declared as native, the JVM arranges for a native method call to result in execution passing to native library code.


synchronized

The use of the synchronized keyword in a method declaration ensures that at a time only one thread can execute the block of statements following the method declaration. Other threads wanting access to the method are forced to wait until the currently executing thread returns from the method.

transient

Only variables can be declared as transient. When used as a modifier in a variable declaration, it suggests that a variable may not be written out when the class is serialized.

volatile

The keyword volatile can be used to declare variables. The use of the keyword volatile in a variable declaration suggests the compiler that multiple threads may access the variable. Therefore, the value of the variable may change unexpectedly. A compile-time error will occur on declaring a variable both volatile and final.

Order of appearance of modifiers:

If two or more modifiers appear in the declaration of a feature, they both affect the functionality of the feature. The order of appearance has no effect on the functionality of the feature. For example, public static means the same as static public.


Rating:



Other articles

Click here to Article home

 
uCertify.com | Our Company | Articles | Privacy | Security | Contact Us | News and Press Release | uCertify India
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.