Various modifires
available in java
There are two types of modifiers in java:
1. Access modifier
2. Non-access modifier
Access Modifiers
Access modifiers
are applicable only on variable, method, constructor, Class and Interface.
Access modifiers are used to
specify the accessibility of a variable, method, constructor , Class and
Interface.
There
are 4 types of access modifiers:
1. private
2. default
3. protected
4. public
1) private
1.
The private access modifier is accessible only within class.
2.
If we are accessing the
private members (data, methods) from outside the class, then there is compile
time error.
3.
If we want to access
private data members from outside the class, we can do it by using public
setters and getters only.
4.
If you make any class constructor
as private, you cannot create the instance of that class from outside the
class.
5.
Private method can’t be
overridden.
6.
A class cannot be private
or protected except nested class.
2) default
If you don't use any modifier, it is
treated as default by default.
1.
The default
modifier is accessible only within package.
2.
If we are accessing
the default members (data, methods) , constructor, class, from outside the package, then there is compile time
error.
|
3) protected
1. The protected access modifier is accessible within
package and outside the package but through inheritance only.
2. The protected access modifier can be applied on the data member, method and constructor.
It can't be applied on the class.
|
4) public
1. The public access modifier is accessible everywhere. It
has the widest scope among all other modifiers.
2.
There can be only one public class in a java
source file and it must be saved by the public class name.
Understanding all java
access modifiers
Let's understand the access modifiers by a simple table.
|
Note:
If you are overriding
any method, overridden method (i.e. declared in subclass) must not be more
restrictive.
Non-access modifier
There are many non-access modifiers
such as:
1. static,
2. abstract,
3. synchronized,
4. native,
5. volatile,
6. transient
7.
Final
8.
strictfp
Final
1. it prevents its content from being modified.
2. You cannot declare a
final method as abstract. Because abstract method must be implemented in
subclasses. But final methods can never be overridden (in subclasses) making it
impossible to implement them in the subclass
Transient
modifier
1. When an instance
variable is declared as transient, then its value doesn't persist when an
object is serialized
Synchronized
modifier
1. When a method is
synchronized it can be accessed by only one thread at a time.
Volatile modifier
1. Volatile modifier tells
the compiler that the volatile variable can be changed unexpectedly by other
parts of your program.
2. Volatile variables are
used in case of multithreading program
transient
What
is serialization?
Serialization is the process of making the object's state
persistent. That means the state of the object is converted into a stream of
bytes and stored in a file. In the same way, we can use the deserialization to
bring back the object's state from bytes. This is one of the important concepts
in Java programming because serialization is mostly used in networking
programming. The objects that need to be transmitted through the network have
to be converted into bytes. For that purpose, every class or interface must
implement the Serialization interface. It is a marker interface without any
methods.
Now
what is the transient keyword and its purpose?
1. By default, all of
object's variables get converted into a persistent state.
2. In some cases, you may want to avoid
persisting some variables because you don't have the need to persist those
variables.
3. So you can declare
those variables as transient.
4. If the variable is declared as transient, then
it will not be persisted.
Note:
|
1.
You can use more-than-one modifier while declaring
class or its members.
2.
When you do so, the modifiers may appear in any order
in that declaration.
3.
For example, you can apply the access modifiers either before
or after or in between the other modifiers
strictfp
The strictfp keyword ensures that you will get the same
result on every platform if you perform operations in the floating-point
variable. The precision may differ from platform to platform that is why java
programming language have provided the strictfp keyword, so that you get same
result on every platform. So, now you have better control over the
floating-point arithmetic.
|
Modifiers table
Modifiers
|
class
|
interface
|
methods
|
variable
|
constructor
|
block
|
||
outer
|
inner
|
outer
|
inner
|
|||||
public
|
y
|
y
|
y
|
y
|
y
|
y
|
y
|
|
Private
|
|
y
|
|
y
|
y
|
y
|
y
|
|
protected
|
|
y
|
|
y
|
y
|
y
|
y
|
|
<default>
|
y
|
y
|
y
|
y
|
y
|
y
|
y
|
|
Final
|
y
|
y
|
|
|
y
|
y
|
|
|
abstract
|
y
|
y
|
y
|
y
|
y
|
|
|
|
static
|
|
y
|
|
y
|
y
|
y
|
|
y
|
syncronized
|
|
|
|
|
y
|
|
|
y
|
native
|
|
|
|
|
y
|
|
|
|
strictfp
|
y
|
y
|
y
|
y
|
y
|
|
|
|
transient
|
|
|
|
|
|
y
|
|
|
volatile
|
|
|
|
|
|
y
|
|
|
Imp Conclusions:
1. The modifiers which are
applicable for inner classes but not for outer classes are private, protected, static.
2. The modifiers which are
applicable for classes but not for interface are final.
3. The modifiers which are
applicable for classes but not for enums are final and abstract.
4. The modifiers which are
applicable only for methods and which we can’t use anywhere else is native
5. The only modifiers
which are applicable for constructors are.
public ,private, protected, default
public ,private, protected, default
6. The only applicable
modifier for local variable is final.
Important truths
1.
The interface which is declared inside a class is always static whether we are declaring or not.
2.
The interface which is declared inside interface is always public and static whether we are
declaring or not.
3.
The class which is declared inside interface is always public and static whether we are
declaring or not.