| Modifiers are Java keywords that are used to | | | | keyword as a modifier are called static |
| declare features in applications. They affect | | | | variables. They are not attached to a |
| either the lifetime or the accessibility of a | | | | particular instance of a class but rather |
| feature. A feature may be a class, a method, | | | | belong to a class as a whole. They are |
| or a variable. | | | | declared using the following syntax:static |
| | | | type varIdentifier;where, the name of the |
| Modifiers that affect the visibility of a | | | | variable is varIdentifier and its data type |
| feature are called access modifiers or | | | | is type. |
| visibility modifiers. The remaining modifiers | | | | |
| do not fall into any clear categorization and | | | | Static variables that are not explicitly |
| may be called storage and lifetime modifiers. | | | | initialized in the code are automatically |
| | | | initialized with a default value. The default |
| The Access Modifiers | | | | value depends on the data type of the |
| | | | variables. |
| Access modifiers control the access of a | | | | |
| feature. They enable a programmer to | | | | Methods declared with the keyword static as |
| determine whether access to a feature is | | | | modifier are called static methods or class |
| limited to a particular class, a class and | | | | methods. They are so called because they |
| its subclass, or a package, or if it is | | | | affect a class as a whole and not a |
| accessible without any restriction. Following | | | | particular instance of the class. Static |
| are the features of a class: | | | | methods are always invoked without reference |
| | | | to a particular instance of a class. |
| The class itself | | | | |
| | | | A static method suffers from the following |
| Its class variable | | | | restrictions: |
| | | | |
| Its methods and constructors | | | | A static method can only call other static |
| | | | methods. |
| Java provides three access modifiers: public, | | | | |
| protected, and private. If none of the three | | | | A static method must only access static data. |
| access modifiers is used to declare a | | | | |
| feature, Java provides a fourth modifier for | | | | A static method cannot reference to the |
| which there is no name. It may be called a | | | | current object using keywords super or this. |
| default access, a package access, or a | | | | |
| friendly access modifier. A feature may be | | | | If a method is not declared as static, it is |
| declared using at most one access modifier. A | | | | called an instance method. Instance methods |
| compile-time error will occur if more than | | | | are associated with objects and not with |
| one access modifier is used to declare a | | | | classes. Furthermore, they can access all the |
| feature. The access modifiers are briefly | | | | variables in a class whether they are |
| described as follows:private | | | | instance fields or class fields. |
| | | | |
| The private modifier is the most restrictive | | | | Non-static variables cannot be accessed from |
| access modifier. It limits the visibility of | | | | within a static method. Therefore, the |
| methods and instance variables to the class | | | | following code will not compile |
| in which they are defined. An instance | | | | successfully:public class ClsStat{int |
| variable declared as private is visible (or | | | | x;public static void main(String argv[]){ |
| accessible) only by methods within the same | | | | |
| class, but any other classes or objects | | | | System.out.println(x); //a compile-time error |
| 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. | | | | |
| | | | In the given code a compile-time error will |
| A) default access | | | | occur. As the main method is static, it |
| | | | cannot access the non-static field (i.e., x) |
| When none of the three access modifiers is | | | | in the ClsStat class. |
| used to declare a feature, the default access | | | | |
| modifier is provided by Java. This modifier | | | | The static modifier can be used to declare a |
| enables a feature to be accessed by any other | | | | nested class. A nested class declared as |
| classes, provided that the classes are in the | | | | static is called a static nested class. A |
| same package in which the class that defines | | | | static nested class can directly access only |
| them belongs to. Variables or methods | | | | static variables and methods defined in its |
| declared without any access modifiers are | | | | enclosing class. However, it cannot refer |
| accessible to all the other classes in the | | | | directly to instance variables or methods |
| same package in which the class containing | | | | defined in its enclosing class. It can refer |
| them belongs to. | | | | them only through an object reference. |
| | | | |
| B) protected | | | | A static initializer is a block of code, |
| | | | enclosed within curly brackets and labeled as |
| An intermediate level of access between | | | | static. It is not a part of a method. A class |
| package and private is protected. Methods or | | | | may have one or more static initializers. All |
| variables declared as protected are | | | | the static initializers in a class are |
| accessible to all classes within the same | | | | executed exactly once and in the same order |
| package in which the class that defines them | | | | in which they appear in the class. The |
| belongs to. They are also accessible to | | | | execution of static initializers takes place |
| subclasses of the class that have been | | | | at the class load time.final |
| defined outside the package. | | | | |
| | | | The final modifier can be used to declare |
| C) public | | | | classes, methods, and variables. |
| | | | |
| The most unrestrictive access modifier is | | | | A class can be declared as final if its |
| public. It can be used to declare a class, a | | | | definition is complete and no subclasses are |
| method, or a variable. A class declared as | | | | needed. The use of the final keyword in a |
| public is accessible in any program without | | | | class declaration suggests that the class |
| any restriction. It also enables a method, a | | | | cannot be subclassed. In other words, a final |
| constructor, or a variable to be accessed | | | | class cannot have any subclasses. For |
| anywhere in a program where its class can be | | | | example, the following class declaration will |
| accessed. | | | | result in a compile-time error: |
| | | | |
| Access modifiers are generally used to | | | | Class MyClass extends Math{ |
| declare class level variables. As variables | | | | |
| declared within a method can only be used | | | | //other code |
| within its enclosing method, they may not | | | | |
| have access modifiers. | | | | } |
| | | | |
| Other modifiers | | | | As the java.lang.Math class is final, its |
| | | | name cannot appear in the extends clause of |
| 1) abstract | | | | another class declaration. Declaring a class |
| | | | as final implicitly makes all the methods in |
| The abstract modifier can be used to declare | | | | the class as final. Therefore, methods of a |
| classes and methods. A class declared as | | | | final class are never overridden |
| abstract is a partially implemented class. A | | | | |
| class is declared as abstract when it | | | | When a method is declared as final, it cannot |
| contains one or more abstract methods. A | | | | be overridden or hidden by a subclass. In |
| class is said to contain abstract methods in | | | | other words, a subclass cannot introduce a |
| any of the following conditions: | | | | new version of the method. Methods declared |
| | | | as private and all the methods declared in a |
| The body of the class explicitly declares an | | | | final class are implicitly final, and there |
| abstract method. | | | | is no need to explicitly declare them as |
| | | | final. |
| The class inherits one or more abstract | | | | |
| methods from its abstract superclass for | | | | Variables declared with the keyword final as |
| which it does not provide implementations. | | | | a modifier are called final variables. They |
| | | | are so called because once initialized a |
| The class directly inherits an interface that | | | | value, their values cannot be modified. Any |
| contains an abstract method but the class | | | | attempt made to modify the value of a |
| neither declares the method nor does it | | | | pre-initialized final variable will result in |
| inherit a method that implements it. | | | | a compile-time error. |
| | | | |
| If a class containing an abstract method is | | | | A final variable is declared as shown |
| not declared as abstract, a compile-time | | | | below:final double IAMFINAL=0;where, IAMFINAL |
| error will occur. A class can also be | | | | is the name of variable whose data type is |
| declared as abstract even if it has no | | | | double. This statement declares the variable |
| abstract methods. | | | | IAMFINAL as final and initializes it with the |
| | | | value 0 at the same time. Further attempts to |
| As the implementation of an abstract class is | | | | change the value of IAMFINAL will result in a |
| incomplete, it is not possible to create an | | | | compile-time error.native |
| instance of an abstract class. However, | | | | |
| subclasses of an abstract class that itself | | | | Methods implemented in a language other than |
| is not declared as abstract can be | | | | Java are declared using the native keyword as |
| instantiated. Subclasses that extend an | | | | modifier. These methods are declared by using |
| abstract class are responsible for | | | | the native keyword. The body of a native |
| implementing the unimplemented part of the | | | | method is replaced by a semicolon, which |
| abstract class. A class declared as abstract | | | | indicates that the implementation of the |
| may also contain non-abstract methods. | | | | method is omitted. When a method is declared |
| | | | as native, the JVM arranges for a native |
| An abstract method is a method without | | | | method call to result in execution passing to |
| implementation, i.e., an abstract is not | | | | native library code.synchronized |
| defined in the class in which it is declared. | | | | |
| The following points must be noted about the | | | | The use of the synchronized keyword in a |
| declaration of an abstract method: | | | | method declaration ensures that at a time |
| | | | only one thread can execute the block of |
| An abstract method declaration provides the | | | | statements following the method declaration. |
| signature of the method, return type, and | | | | Other threads wanting access to the method |
| throws clause (if any), but it does not | | | | are forced to wait until the currently |
| provide an implementation of the method. The | | | | executing thread returns from the |
| declaration of an abstract method ends with a | | | | method.transient |
| semicolon rather than a block of code. | | | | |
| | | | Only variables can be declared as transient. |
| The class in which the abstract method is | | | | When used as a modifier in a variable |
| declared must itself be declared as abstract. | | | | declaration, it suggests that a variable may |
| | | | not be written out when the class is |
| The use of the abstract keyword in a method | | | | serialized.volatile |
| declaration along with the modifiers: final, | | | | |
| static, native, synchronized, or private is | | | | The keyword volatile can be used to declare |
| not permitted. | | | | variables. The use of the keyword volatile in |
| | | | a variable declaration suggests the compiler |
| By default, a method is non-abstract and it | | | | that multiple threads may access the |
| requires a block of code after the | | | | variable. Therefore, the value of the |
| declaration part. | | | | variable may change unexpectedly. A |
| | | | compile-time error will occur on declaring a |
| 2) static | | | | variable both volatile and final. |
| | | | |
| The static modifier can be used to declare an | | | | Order of appearance of modifiers: |
| inner class, a method, and a variable. Static | | | | |
| means one per class, not one for each object | | | | If two or more modifiers appear in the |
| no matter how many instances of the class | | | | declaration of a feature, they both affect |
| might exist. This means that static features | | | | the functionality of the feature. The order |
| can be used without creating an instance of a | | | | of appearance has no effect on the |
| class. | | | | functionality of the feature. For example, |
| | | | public static means the same as static |
| Variables declared by using the static | | | | public. |