Important Keywords in Java
20:00:00
In the
Java programming language, a keyword is one of 51 reserved words that have a
predefined meaning in the language; because of this, programmers cannot use
keywords as names for variables, methods, classes, or as any other identifier.
But some
important keywords should known as java developer,
There
are,
Final
Static
This
super
static
Synchronize
volatile
Final keyword in java
It is used to make a variable as a constant, Restrict method
overriding, Restrict inheritance. It is used at variable level, method level
and class level. In java language final keyword can be used in following way.
• Final at
variable level
• Final at
method level
• Final at
class level
Static keyword in java
The static keyword is used in java mainly for memory
management. It is used with variables, methods, blocks and nested class. It is
a keyword that are used for share the same variable or method of a given class.
This is used for a constant variable or a method that is the same for every
instance of a class. The main method of a class is generally labeled static.
No object needs to be created to use static variable or call
static methods, just put the class name before the static variable or method to
use them. Static method can not call non-static method.
In java language static keyword can be used for following
• variable
(also known as class variable)
• method
(also known as class method)
• block
• nested
class
Difference between static and final keyword
static keyword always fixed the memory that means that will
be located only once in the program where as final keyword always fixed the
value that means it makes variable values constant.
Note: As for as real time statement there concern every
final variable should be declared the static but there is no compulsion that
every static variable declared as final.
This keyword in java
this is a reference variable that refers to the current
object. It is a keyword in java language represents current class object
Usage of this keyword
• It can be
used to refer current class instance variable.
• this()
can be used to invoke current class constructor.
• It can be
used to invoke current class method (implicitly)
• It can be
passed as an argument in the method call.
• It can be
passed as argument in the constructor call.
• It can
also be used to return the current class instance.
Why use this keyword in java ?
The main purpose of using this keyword is to differentiate
the formal parameter and data members of class, whenever the formal parameter
and data members of the class are similar then jvm get ambiguity (no clarity
between formal parameter and member of the class)
To differentiate between formal parameter and data member of
the class, the data member of the class must be preceded by "this".
"this" keyword can be use in two ways.
• this .
(this dot)
• this()
(this off)
this . (this dot)
which can be used to differentiate variable of class and
formal parameters of method or constructor.
"this" keyword are used for two purpose, they are
• It always
points to current class object.
• Whenever
the formal parameter and data member of the class are similar and JVM gets an
ambiguity (no clarity between formal parameter and data members of the class).
Super keyword in java
Super keyword in java is a reference variable that is used
to refer parent class object. Super is an implicit keyword create by JVM and
supply each and every java program for performing important role in three
places.
• At variable
level
• At method
level
• At
constructor level
Need of super keyword:
Whenever the derived class is inherits the base class
features, there is a possibility that base class features are similar to
derived class features and JVM gets an ambiguity. In order to differentiate
between base class features and derived class features must be preceded by
super keyword.
Synchronized Keyword in Java
Synchronized Keyword is used for when we want to allowed
only one thread at a time then use Synchronized modifier. If a method or block
declared as a Synchronized then at a time only one thread is allowed to operate
on the given object.
Synchronized is a Modifier which is applicable for the
method or block, we can not declare class or variable with this modifier.
Advantage of Synchronized
The main advantage of Synchronized keyword is we can resolve
data inconsistency problem.
Dis-Advantage of Synchronized
The main dis-advantage of Synchronized keyword is it
increased the waiting time of thread and effect performance of the system,
Hence if there is no specific requirement it is never recommended to use
synchronized keyword.
Volatile Keyword in Java
If the variable keep on changing such type of variables we
have to declare with volatile modifier. Volatile is a modifier applicable only
for variables but not for method and class.
If a variable declared as volatile then for every thread a
separate local copy will be created. Every intermediate modification performed
by that thread will takes place in local copy instead of master copy. Once the
value got finalized just before terminating the thread the master copy value
will be updated with local stable value.
Advantage of Volatile
The main advantage of Volatile keyword is we can resolve
data inconsistency problems.
Dis-Advantage of Volatile
The main dis-advantage of Volatile keyword is, crating and
maintaining a separate copy for every thread, increases complexity of the
programming and effects performance of the system. Hence if there is no
specific requirement it is never recommended to use volatile keyword, and it is
almost outdated keyword.
Note: Volatile variable means its value keep on changing
where as final variable means its value never changes. Hence final-Volatile
combination is illegal combination for variables.
0 comments