0% found this document useful (0 votes)
7 views3 pages

Modifiers

The document outlines two types of modifiers in programming: access modifiers and non-access modifiers. Access modifiers (private, default, protected, public) define the scope and accessibility of properties, while non-access modifiers (static, final, abstract, synchronized, transient, volatile, strictfp) apply conditions that affect program behavior. Each modifier has specific purposes and usage contexts, such as memory management, abstraction, synchronization, and ensuring consistent results across threads.

Uploaded by

Ranganath G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Modifiers

The document outlines two types of modifiers in programming: access modifiers and non-access modifiers. Access modifiers (private, default, protected, public) define the scope and accessibility of properties, while non-access modifiers (static, final, abstract, synchronized, transient, volatile, strictfp) apply conditions that affect program behavior. Each modifier has specific purposes and usage contexts, such as memory management, abstraction, synchronization, and ensuring consistent results across threads.

Uploaded by

Ranganath G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MODIFIERS

There are two types of modifiers, they are:


● Access Modifiers
● Non-access Modifiers

ACCESS MODIFIERS:
The main purpose of these modifiers will be to provide the scope and boundaries
of the properties like upto how far and where we can and cannot access them.
The following are the access modifiers:
● Private
● Default
● Protected
● Public
Private:
● The Scope of this keyword is we can access the properties only within a class
but we cannot access them outside of the class, even if we implement the
inheritance concept.
● We can use this keyword at a variable level method level, constructor level, and
inner classes level.
● It is also known as native access modifier
Default:
● The scope of this modifier is we can access the properties only within a package
but not outside the package.
● This keyword will be there by default at the variable, method, constructor, class,
and interface level but we should not use this explicitly.
● We can use this keyword only at the defined method of the interface (java 8
version and above)
Protected:
● The scope of this modifier is we can access the properties outside of the
package but only through the subclass.
● We can use this keyword at variable, method, constructor, and inner class levels.
● It is also known as an inherited access modifier.
Public:
● The scope of these modifiers is we can access the properties anywhere.
● We can use this keyword at the variable, method, constructor, class, and
interface levels.
● It is also known as universal access modifier.

NON ACCESS MODIFIERS


● The primary purpose of these modifiers is to allow us to apply conditions then
based on those conditions the program behavior will depend
● Following are the non-access modifiers:
=> static => synchronized
=> final => transient
=> abstract => volatile
=> strictfp
● Static:
1. the main purpose of static keyword is to implement either common
operations or one-time operations.
2. it will give us efficient memory management.
3. we can use the static keyword at variable, method, inner classes, and
block levels.
4. for these properties, memory space will be allocated only once at the time
of compilation and we need not create objects to invoke these properties.

● Final:
1. the main purpose of the final keyword is to make things constant.
2. we can use this keyword at variable, method, and class levels.
3. if we use this keyword at a variable level, then we cannot change its value
4. if we use this keyword at the method level, then that method can be
overloaded but not overridden
5. if we use this keyword at the class level then that class cannot be
inherited
EG:
● class X
{
final int a =10;
void display()
{
a=a+10; //Wrong
Int b=a+10; //Correct
SOP(b);
}
}
● Class A
{
final void m1(int a)
{
SOP (a); //Correct
}
void m1(String a)
{
SOP(a);
}
}
class B extends A
{
void m1(int a) //Wrong
{
SOP(a);
}
}
● Abstract:
1. The main purpose of this keyword is to implement abstraction.
2. To achieve abstraction we have two ways, by using abstract classes and
by using interfaces.
3. We can use this keyword at the class level and method level.

● Synchronized:
a. The main purpose of this keyword is used to implement the
synchronization concept
b. Actually, the main purpose of multi-threading will provide a parallel flow of
execution
c. If multiple threads are running parallelly on the same resource then, we
will get inconsistent results.
d. To get consistent results, we need to apply the synchronization concept
e. Synchronization is the process of allowing thread by thread Execution
instead of the parallel or concurrent flow of Execution of threads so that by
implementing a locking and unlocking mechanism, each and every thread
results are going to be added to each other but not replaced
f. We can use the synchronized keyword at method and block levels

● Transient:
1. The primary purpose of this keyword is used to restrict not being
serializable of any field (variable) or binding with setter and getter methods
2. Serialization: it is the process of transferring n number of bytes of data in
terms of one object into an external file like a .txt file
3. De-Serialization: It is a process of reading n number of bytes of data in
terms of one object from an external file to a Java application.
4. We can use the transient keyword at the variable level only.

● Volatile:
1. The main purpose of this keyword is to get consistent results whenever
multiple threads are running parallel.
2. It is an alternate option for synchronization.
3. We can use this keyword at a variable level only.

● StrictFP:
1. The main purpose of this keyword is used to maintain the same
floating-point range in each and every OS
2. We can use this keyword at class, interface, and method levels.

You might also like