You are on page 1of 15

Modifiers in java…

Presented by :- komal s. wagh


Modifiers in java…

 Java provides a rich set of modifiers. They are used to control access mechanisms and also provide
information about class functionalities to JVM. They are divided into two categories namely:
1. Access modifiers
2. Non-access modifiers
Need of modifiers…
 Whenever we are writing our own classes compulsory we have to provide some information about
our class to the jvm. Like
1. Whether this class can be accessible from anywhere or not.
2. Whether child class creation is possible or not.
3. Whether object creation is possible or not etc. We can specify this information by using the
corresponding modifiers.
Modifiers…

 Access modifiers  Non-Access modifiers


1. Public 1. Static
2. Protected 2. Final
3. Default 3. Abstract
4. private 4. Synchronized
5. Transient
6. Volatile
7. native
Access modifiers…
1.public:
When a member of a class is modified by public, then that member can be accessed by any other code.

2.private:
 When a member of a class is specified as private, then that member can only be accessed by other members
of its class. 

3.default:
It is also referred to as no modifier. Whenever we do not use any access modifier it is treated as default where
this allows us to access within a class, within a subclass, and also non-sun class within a package but when the
package differs now be it a subclass or non-class we are not able to access. 
4.proteced:
With the above default keyword we were facing an issue as we are getting closer to the real world with the
above default modifier but there was a constriction as we are not able to access class sub-class from a different
package. So protected access modifier allows not only to access class be it subclass or non-sub class but allows
us to access subclass of the different package which brings us very close to a real-world and hence strong
understanding of inheritance is required for understanding and implementing this keyword. 
Non-access modifiers…..
 Non-access modifiers provide information about the characteristics of a class, method, or variable to the
JVM. Seven types of Non-Access modifiers are present in Java. They are –
1. Static
2. Final
3. Abstract
4. Synchronized
5. Transient
6. Volatile
7. native
1.static
 The static keyword means that the entity to which it is applied is available outside any particular
instance of the class. That means the static methods or the attributes are a part of the class and not an
object.
 The memory is allocated to such an attribute or method at the time of class loading. The use of a static
modifier makes the program more efficient by saving memory.
 A static field exists across all the class instances, and without creating an object of the class, they can
be called.
2.final

 The final keyword indicates that the specific class cannot be extended or a method cannot be
overridden.
 The final modifier apply to the to variable
1.if variable is primitive type :- Value will be fixed
2.if variable is reference type :- reference will get fixed but value can be
change
3.abstract
 abstract keyword is used to declare a class as partially implemented means an object cannot be created
directly from that class.
 Any subclass needs to be either implement all the methods of the abstract class, or it should also need to
be an abstract class.
 The abstract keyword cannot be used with static, final, or private keywords because they prevent
overriding, and we need to override methods in the case of an abstract class.
4.synchronized
1. Synchronized is the modifier applicable for methods and blocks but not for variables and classes.
2. If a method or block declared with synchronized keyword then at a time only one thread is allow to
execute that method or block on the given object.
3. The main advantage of synchronized keyword is we can resolve data inconsistency problems.
4. But the main disadvantage is it increases waiting time of the threads and effects performance of the
system. Hence if there is no specific requirement never recommended to use synchronized keyword. For
synchronized methods compulsory implementation should be available , but for abstract methods
implementation won't be available , Hence abstract - synchronized combination is illegal for methods.
5.Transient
1. Transient is the modifier applicable only for variables but not for methods and classes.
2. At the time of serialization if we don't want to serialize the value of a particular variable to meet the
security constraints then we should declare that variable with transient modifier.
3. At the time of serialization jvm ignores the original value of the transient variable and save default value
that is transient means "not to serialize".
4. Static variables are not part of object state hence serialization concept is not applicable for static
variables duo to this declaring a static variable as transient there is no use.
5. Final variables will be participated into serialization directly by their values due to this declaring a final
variable as transient there is no impact.
6.volatile
1. Volatile is the modifier applicable only for variables but not for classes and methods.
2. If the value of variable keeps on changing such type of variables we have to declare with volatile modifier.
3. If a variable declared as volatile then for every thread a separate local copy will be created by the jvm, all
intermediate modifications performed by the thread will takes place in the local copy instead of master copy.
4. Once the value got finalized before terminating the thread that final value will be updated in master copy.
5. The main advantage of volatile modifier is we can resolve data inconsistency problems, but creating 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 never recommended to use volatile
modifier and it's almost outdated.
6. Volatile means the value keep on changing where as final means the value never changes hence final volatile
combination is illegal for variables
7.native
Native is a modifier applicable only for methods but not for variables and classes.
 The methods which are implemented in non java are called native methods or foreign methods. The main
objectives of native keyword are:
1. To improve performance of the system.
2. To use already existing legacy non-java code.
3. To achieve machine level communication(memory level - address)  Pseudo code to use native
keyword in java.
Thank you…

You might also like