



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Modifiers are keywords that are added to change meaning of a definition. In Java, modifiers are catagorized into two types,
Java language has four access modifier to control access levels for classes, variable methods and constructor.
Default : Default has scope only inside the same package Public : Public scope is visible everywhere Protected : Protected has scope within the package and all sub classes Private : Private has scope only within the classes
Non access modifiers do not change the accessibility of variables and methods, but they do provide them special properties. Non access modifiers are of 5 types,
Final
Final modifier is used to declare a field as final i.e. it prevents its content from being modified. Final field must be initialized when it is declared.
Example :
class Cloth { final int MAX_PRICE = 999; //final variable final int MIN_PRICE = 699; final void display() //final method { System.out.println("Maxprice is" + MAX_PRICE ); System.out.println("Minprice is" + MIN_PRICE); } }
A class can also be declared as final. A class declared as final cannot be inherited. String class in java.lang package is a example of final class. Method declared as final can be inherited but you cannot override(redefine) it.
Static Modifier
Static Modifiers are used to create class variable and class methods which can be accessed without instance of a class. Lets study how it works with variables and member functions.
Static with Variables
Static variables are defined as a class member that can be accessed without any object of that class. Static variable has only one single storage. All the object of the class having static variable will have the same instance of static variable. Static variables are initialized only once.
Static variable are used to represent common property of a class. It saves memory. Suppose there are 100 employee in a company. All employee have its unique name and employee id but company name will be same all 100 employee. Here company name is the common property. So if you create a class to store employee detail, company_name field will be mark as static.
Example
class Employee {
Static variable vs Instance Variable
Static variable Instance Variable
Represent common property Represent unique property
Accessed using class name Accessed using object
get memory only once get new memory each time a new object is created
Example
public class Test { static int x = 100; int y = 100; public void increment() { x++; y++; } public static void main( String[] args ) { Test t1 = new Test(); Test t2 = new Test(); t1.increment(); t2.increment(); System.out.println(t2.y); System.out.println(Test.x); //accessed without any instance of class.
Output : 101 102
See the difference in value of two variable. Static variable y shows the changes made to it by increment() method on the different object. While instance variable x show only the change made to it by increment() method on that particular instance.
Static Method
A method can also be declared as static. Static methods do not need instance of its class for being accessed. main() method is the most common example of static method. main() method is declared as static because it is called before any object of the class is created.
Example :
class Test {
public static void square(int x) { System.out.println(x*x); }
public static void main (String[] arg) {
square(8) //static method square () is called without any instance of class. } }
Output: 64
Static block
Static block is used to initialize static data member. Static block executes before main() method.
Example
class ST_Employee { int eid; String name; static String company_name;
static {
int x; public static void main(String[] args) { Test tt=new Test(); tt.x=10; //works fine with instance of class } }
Q. Why main() method is static in java?
Because static methods can be called without any instance of a class and main() is called before any instance of a class is created.
Transient modifier
When an instance variable is declared as transient, then its value doesn't persist when an object is serialized
Synchronized modifier
When a method is synchronized it can be accessed by only one thread at a time. We will discuss it in detail in Thread.
Volatile modifier
Volatile modifier tells the compiler that the volatile variable can be changed unexpectedly by other parts of your program. Volatile variables are used in case of multithreading program.