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