Do you have psychic powers


Modifiers

Modifiers are Java keywords that are used tokeyword as a modifier are called static
declare features in applications. They affectvariables. They are not attached to a
either the lifetime or the accessibility of aparticular 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 avariable is varIdentifier and its data type
feature are called access modifiers oris  type.
visibility modifiers. The remaining modifiers
do not fall into any clear categorization andStatic 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  Modifiersvalue depends on the data type of the
variables.
Access modifiers control the access of a
feature. They enable a programmer toMethods declared with the keyword static as
determine whether access to a feature ismodifier are called static methods or class
limited to a particular class, a class andmethods. They are so called because they
its subclass, or a package, or if it isaffect a class as a whole and not a
accessible without any restriction. Followingparticular 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  variablerestrictions:
Its  methods  and  constructorsA static method can only call other static
methods.
Java provides three access modifiers: public,
protected, and private. If none of the threeA static method must only access static data.
access modifiers is used to declare a
feature, Java provides a fourth modifier forA static method cannot reference to the
which there is no name. It may be called acurrent  object using keywords super or this.
default access, a package access, or a
friendly access modifier. A feature may beIf a method is not declared as static, it is
declared using at most one access modifier. Acalled an instance method. Instance methods
compile-time error will occur if more thanare associated with objects and not with
one access modifier is used to declare aclasses. Furthermore, they can access all the
feature. The access modifiers are brieflyvariables in a class whether they are
described  as  follows:privateinstance  fields  or  class  fields.
The private modifier is the most restrictiveNon-static variables cannot be accessed from
access modifier. It limits the visibility ofwithin a static method. Therefore, the
methods and instance variables to the classfollowing code will not compile
in which they are defined. An instancesuccessfully:public class ClsStat{int
variable declared as private is visible (orx;public  static  void  main(String  argv[]){
accessible) only by methods within the same
class, but any other classes or objectsSystem.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  accessoccur. As the main method is static, it
cannot access the non-static field (i.e., x)
When none of the three access modifiers isin  the  ClsStat  class.
used to declare a feature, the default access
modifier is provided by Java. This modifierThe static modifier can be used to declare a
enables a feature to be accessed by any othernested class. A nested class declared as
classes, provided that the classes are in thestatic is called a static nested class. A
same package in which the class that definesstatic nested class can directly access only
them belongs to. Variables or methodsstatic variables and methods defined in its
declared without any access modifiers areenclosing class. However, it cannot refer
accessible to all the other classes in thedirectly to instance variables or methods
same package in which the class containingdefined in its enclosing class. It can refer
them  belongs  to.them  only  through  an  object  reference.
B)  protectedA static initializer is a block of code,
enclosed within curly brackets and labeled as
An intermediate level of access betweenstatic. It is not a part of a method. A class
package and private is protected. Methods ormay have one or more static initializers. All
variables declared as protected arethe static initializers in a class are
accessible to all classes within the sameexecuted exactly once and in the same order
package in which the class that defines themin which they appear in the class. The
belongs to. They are also accessible toexecution of static initializers takes place
subclasses of the class that have beenat  the  class  load  time.final
defined  outside  the  package.
The final modifier can be used to declare
C)  publicclasses,  methods,  and  variables.
The most unrestrictive access modifier isA class can be declared as final if its
public. It can be used to declare a class, adefinition is complete and no subclasses are
method, or a variable. A class declared asneeded. The use of the final keyword in a
public is accessible in any program withoutclass declaration suggests that the class
any restriction. It also enables a method, acannot be subclassed. In other words, a final
constructor, or a variable to be accessedclass cannot have any subclasses. For
anywhere in a program where its class can beexample, the following class declaration will
accessed.result  in  a  compile-time  error:
Access modifiers are generally used toClass  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  modifiersAs the java.lang.Math class is final, its
name cannot appear in the extends clause of
1)  abstractanother class declaration. Declaring a class
as final implicitly makes all the methods in
The abstract modifier can be used to declarethe class as final. Therefore, methods of a
classes and methods. A class declared asfinal  class  are  never  overridden
abstract is a partially implemented class. A
class is declared as abstract when itWhen a method is declared as final, it cannot
contains one or more abstract methods. Abe overridden or hidden by a subclass. In
class is said to contain abstract methods inother 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 anfinal 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 forVariables 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 thatvalue, their values cannot be modified. Any
contains an abstract method but the classattempt made to modify the value of a
neither declares the method nor does itpre-initialized final variable will result in
inherit  a  method  that  implements  it.a  compile-time  error.
If a class containing an abstract method isA final variable is declared as shown
not declared as abstract, a compile-timebelow:final double IAMFINAL=0;where, IAMFINAL
error will occur. A class can also beis the name of variable whose data type is
declared as abstract even if it has nodouble. 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 ischange the value of IAMFINAL will result in a
incomplete, it is not possible to create ancompile-time  error.native
instance of an abstract class. However,
subclasses of an abstract class that itselfMethods implemented in a language other than
is not declared as abstract can beJava are declared using the native keyword as
instantiated. Subclasses that extend anmodifier. These methods are declared by using
abstract class are responsible forthe native keyword. The body of a native
implementing the unimplemented part of themethod is replaced by a semicolon, which
abstract class. A class declared as abstractindicates 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 withoutmethod call to result in execution passing to
implementation, i.e., an abstract is notnative  library  code.synchronized
defined in the class in which it is declared.
The following points must be noted about theThe 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 thestatements following the method declaration.
signature of the method, return type, andOther threads wanting access to the method
throws clause (if any), but it does notare forced to wait until the currently
provide an implementation of the method. Theexecuting thread returns from the
declaration of an abstract method ends with amethod.transient
semicolon  rather  than  a  block  of  code.
Only variables can be declared as transient.
The class in which the abstract method isWhen 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 methodserialized.volatile
declaration along with the modifiers: final,
static, native, synchronized, or private isThe 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 itthat multiple threads may access the
requires a block of code after thevariable. Therefore, the value of the
declaration  part.variable may change unexpectedly. A
compile-time error will occur on declaring a
2)  staticvariable  both  volatile  and  final.
The static modifier can be used to declare anOrder  of  appearance  of  modifiers:
inner class, a method, and a variable. Static
means one per class, not one for each objectIf two or more modifiers appear in the
no matter how many instances of the classdeclaration of a feature, they both affect
might exist. This means that static featuresthe functionality of the feature. The order
can be used without creating an instance of aof 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 staticpublic.



1 A B C 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85