Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

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
  • Static
  • Abstract
  • Strictfp
  • Native
  • Synchronized
  • Transient
  • Volatile

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.

Conclusion:

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.



Java and Android application developer with more than 3 years experience.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login