
Play Store Application link – https://play.google.com/store/apps/details?id=com.ideepro.java25hours
Learn with our youtube video
Modifiers-
💡Definition-
These are the keywords which modifies properties of classes and fields.
💡Types of modifiers-
1- Access modifiers
2-Non-access modifiers

Quick overview –
Modifier Type | Modifier | Description |
---|---|---|
Access Modifiers | public | Allows the member to be accessed from anywhere. |
protected | Allows the member to be accessed within the package and by subclasses. | |
default | Allows the member to be accessed within the package. This is the default accessibility if no modifier is specified. | |
private | Allows the member to be accessed only within the class. | |
Non-Access Modifiers | abstract | Specifies that the member does not have an implementation and must be implemented by a subclass. |
final | Prevents the member from being overridden or modified. | |
static | Makes the member a class member, rather than an instance member. | |
synchronized | Ensures that only one thread can access the member at a time. | |
transient | Indicates that the member should not be serialized. | |
volatile | Indicates that the member can be modified by multiple threads and its value should be refreshed from memory when accessed. | |
native | Indicates that the method is implemented in native code using the Java Native Interface (JNI). | |
strictfp | Ensures that the floating-point calculations performed by the method are compliant with the IEEE 754 standard. |
💡1-Access modifier-
Modifies accessibility of classes and fields.
-Controls visibility.
(i)Private – only within class
(ii)Default – only within package.
(iii)Protected – within package and outside the package using inheritance
(iv)Public – everywhere

Note- classes can’t be private and protected.
Note- Local variables can’t have any access modifier

code samples are here
1- Program to demonstrate access modifiers on variables

i- Program for class ExUsingVar
package AccessModifiers8; public class ExUsingVar { public int a=10; private int b=20; protected int c=30; int d=40; public static void main(String[] args) { ExUsingVar e1 =new ExUsingVar(); System.out.println(e1.a); System.out.println(e1.b); System.out.println(e1.c); System.out.println(e1.d); } }
ii-Program for class ExUsingVar1
package AccessModifiers8; public class ExUsingVar1 { public static void main(String[] args) { ExUsingVar e1 =new ExUsingVar(); System.out.println(e1.a); System.out.println(e1.b); System.out.println(e1.c); System.out.println(e1.d); } }
iii-Program to show ExUsingVar3
package AccessModifiers8_1; import AccessModifiers8.ExUsingVar; public class ExUsingVar3 { public static void main(String[] args) { ExUsingVar e1 =new ExUsingVar(); System.out.println(e1.a); System.out.println(e1.b); System.out.println(e1.c); System.out.println(e1.d); } }
💡2-Non Access Modifiers- (Final,Static,abstract,Transient,Synchronized,Volatile, native, strict)
💡1-Final-
used to restrict the user.
💡(a)final variable –
can’t change the value

💡(b) final method –
can’t override

💡(c) final class–
can’t inherit

💡2-Static-
-static belongs to class rather than objects.
💡(a) static variable
-gets memory only once at the time of class loading.
-used to refer the common property of all objects
💡(b) static method
– can be invoked without an instance/object
-can access static variable
💡(c) static nested Class
-can access static data members of outer class
-object is created in scope of outer class
💡(d) static block
– executes even before main method
-used to initialize static data members

💡(E) STATIC method in interface-
– Static methods in interface can have body
– Can be invoked on Interface name only
– Can’t be override by inheritance
💡3- Abstract –
-Used in abstraction(oops concept)
-can be applied on-
a- Abstract class
b- Abstract method
💡4-Transient-
– prevents serialization of value.(Io package concept)
-only for variable
💡5-Synchronized-(multi-threading concept)
-prevents sharing by more than one threads at a time
– can be
a- synchronized method
b- synchronized block
💡6-Volatile-(multi-threading concept)
– Allows modification at run-time.
– only for variable

💡7- Native- JNI (Java Native Interface)
– Only for native/foreign methods
– Methods which are implemented in c/c++ are called as native/foreign methods
💡8- Strict – (strictfp as keyword)
– Ensures that result is same for every platform while performing floating point calculations
Interview Questions —
- What is difference between access and non-access modifiers?
- What is default access modifiers, how to use it on class?
- What is difference static and final modifiers?
[…] Step 8:Modifiers in java – 5th hour +code […]