Вы находитесь на странице: 1из 4

Working with Modifiers in Java

In this article we will discuss different types of access modifiers in Java.

Types of Modifiers in Java Programming:

There are many types of Modifiers in Java Programming. And we use them for different purpose in Java Programming. These modifiers are being used inside & outside of classes during the Java programming. In this article we will defined each modifier in detail and also we will use them in Java programming. It is very important to understand the Java Modifiers and their usage in detail. Because sometimes we need to use Java Modifiers in our applications, but we don’t know which modifier to be used and how? There are two categories of Modifiers in Java Programming.

Access Modifiers in Java Programming

Non Access Modifiers in Java Programming

Access Modifiers:

There are almost four types of access modifiers in Java Programming. and these all are used in classes, variable methods and constructors during the classes.

Private modifier in Java Programming

Public modifier in Java Programming

Protected modifier in Java Programming

Default modifier in Java Programming

Private as Access control modifier:

The modifier type Private is very famous from its meaning. Because we should understand it what is Private. We use the keyword or Modifier Private inside of the class commonly. And its scope is limit to inside the classes. Sometimes we don’t want to allow the people/user to use some data or part of the application. So we keep them Private, by using Private modifier they can’t access our Private data. But the Private data will be used just inside the class. Or we can say that where we kept them Private. They will be used only there.

Public as Access control modifier

The modifier type Public is also famous from its meaning. As we know that public means that everyone can access that data. Or which part of the application we want to be public so we use public at there. And then the user or people will be able to access that part or data. It is used inside the class, but the data can be use anywhere of the class.

Protected as Access control modifier:

The protected is used to protected the data which are being used in the package and it’s all sub- classes. We cannot use the protected data outside the package. Because the protected modifier make the data limited inside the package. The protected access modifiers are accessible outside the package just using the inheritance.

Default as Access control modifier:

The default modifier scope is limited just to its package. It can’t be used outside the package, where it is define. We should keep it in our minds that, there is no any kind of key word as default modifier in Java Programming. But when we don’t use any kind of modifier in Java programming in a class, so it is called default modifier. Note : To understand the Access Modifiers in Java Programming, check the listing 1. In which you will find an example which is explaining the access modifiers. How they can be use and how they works? Listing 1: Access Modifiers in Java Programming

public class Student{ public String name = "Ashley"; publicintroll_number = 12309; privateint age = 19; protected float gpa = 4 ; char section = 'A'; public void Information()


System.out.println("Student Records

System.out.printf("Student Name is: %s",name); System.out.printf("\nStudent Age is: %d",age);

System.out.printf("\nStudent Roll Number is: %d",roll_number); System.out.printf("\nStudent GPA is: %f",gpa); System.out.printf("Student Name is: %c",section);



public static void main(String[] args){ Student student = new Student(); student.Information();



Non Access Modifiers:

The non-access modifiers in Java Programming are used to don’t change the condition of the methods, and variables in classes. As we know we just do programming in classes in java programming. When we define any method so it is inside of classes. There are about to 8 non- access modifiers in Java Programming, and we use them in inheritance.









Final Non-Access Modifier in Java Programming:

When we use inheritance in Java programming, so there is a problem sometimes we don’t need overridden in subclass. And we use Final non-access modifier to check the extend feature of the class. When we use the final with a class so the class will not be extended class. And when we use the final with a primitive data type so it will not able to change its value.

Static Non-Access Modifier in Java Programming:

We use static modifier for variables and also for methods. We use static on those variables and methods which belongs to class. And we use the class name to access the static variables or methods. And one static member can call the other static member or variable directly.

Abstract Non-Access Modifier in Java Programming:

We usually use the abstract on class and also on methods. When we use the abstract on any class or a method so its mean that, the abstract class or abstract method will not implement. According the syntax of the abstract modifier the abstract class and abstract method should be end with terminator “;”. When we defined a class as abstract class, so we can’t create its instance. And we should overridden the abstract method with or in subclass. The most important thing is that we can use the abstract class as final. And we also can’t use the methods as static, final and private. We should keep it in our minds during the programming.

Strictfp Non-Access Modifier in Java Programming:

We use the Strictfp non-access modifier on with class and methods. We use it just for confirm the standard of IEEE754, by which it create the floating point independent. And we can’t use the Strictfp in abstract class or methods.

Native Non-Access Modifier in Java Programming:

We just use the native non-access modifier with methods. And the native methods should end with terminator “;”. When we use the native with methods it will not implements more.

Synchronized Non-Access Modifier in Java Programming:

Same like native non-access modifier, we use the synchronized with methods. When we used the synchronized with methods, they will just allow to execute a single thread at a time. We can use the synchronized with any kind of access modifier during the java programming.

Volatile Non-Access Modifier in Java Programming:

We use the volatile on the instance variables. It make the instance variable as volatile, its purpose is just about the memory. As we know the variables are used to take a place at memory to keep data there. The volatile non-access modifier is also used to inform the JVM the thread is accessing the variable, it should always merge its private copy of the variable with its master variable copy at memory. As we know that variables are used to take space in memory to place data over there.

Transient Non-Access Modifiers in Java Programming:

It is same like volatile non-access modifier, we use it just for instance variables. As we can’t use the instance variables as serialized when they are transient. We use them commonly in java database. The transient key word is used to indicate that the instance variable to be skip because it is containing serialized object.


In this tutorial we learnt about the modifiers in Java Programming, as we discussed two types of modifiers in java programming. I.e. access modifier and non-access modifiers and in non-access modifiers we discussed 8 types. These are all used for different purpose on methods, variables, and classes. Every modifier is important in java programming, and these help us to solve problems, during the ideal programming or developing applications. And in listing 1, I used the access modifiers in a simple example. In this tutorial I didn’t use the non-access modifiers. Because these are used in difficult java programs. Which we will use them in my coming tutorials about the java programming.

Read more: http://mrbool.com/working-with-modifiers-in-java/29170#ixzz32XDzMFKm