This document discusses access modifiers and non-access modifiers in Java. There are four types of access modifiers - private, public, protected, and default - that control access to classes, methods, and variables. Non-access modifiers include final, static, abstract, strictfp, native, synchronized, transient, and volatile, which are used for inheritance, memory management, and other purposes. The document provides details on how each modifier works and its proper usage in Java programs.
This document discusses access modifiers and non-access modifiers in Java. There are four types of access modifiers - private, public, protected, and default - that control access to classes, methods, and variables. Non-access modifiers include final, static, abstract, strictfp, native, synchronized, transient, and volatile, which are used for inheritance, memory management, and other purposes. The document provides details on how each modifier works and its proper usage in Java programs.
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 "now which modifier to be used and how# There are two categories of Modifiers in Java Programming. Access Modifiers in Java Programming $on 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. %e use the "eyword or Modifier Private inside of the class commonly. And its scope is limit to inside the classes. &ometimes we don!t want to allow the people'user to use some data or part of the application. &o we "eep them Private by using Private modifier they can!t access our Private data. But the Private data will be used (ust inside the class. )r we can say that where we "ept 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 "now that public means that everyone can access that data. )r 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 pac"age and it!s all sub* classes. %e cannot use the protected data outside the pac"age. Because the protected modifier ma"e the data limited inside the pac"age. The protected access modifiers are accessible outside the pac"age (ust using the inheritance. Default as Access control modifier: The default modifier scope is limited (ust to its pac"age. It can!t be used outside the pac"age where it is define. %e should "eep it in our minds that there is no any "ind of "ey word as default modifier in Java Programming. But when we don!t use any "ind 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 eam!le which is e!laining the access modifiers. "ow they can #e use and how they works$ Listing 1+ Access Modifiers in Java Programming public class Student{ public String name = "Ashley"; publicintroll_number = 12309; priateint age = 19; protected !loat gpa = " ; char section = #A#; public oid $n!ormation%& { System'out'println%"Student (ecords'''"&; System'out'print!%"Student )ame is* +s",name&; System'out'print!%"-nStudent Age is* +d",age&; System'out'print!%"-nStudent (oll )umber is* +d",roll_number&; System'out'print!%"-nStudent ./A is* +!",gpa&; System'out'print!%"Student )ame is* +c",section&; 0 public static oid main%String12 args&{ Student student = ne3 Student%&; student'$n!ormation%&; 0 0 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 "now we (ust do programming in classes in (ava programming. %hen we define any method so it is inside of classes. There are about to , non* access modifiers in Java Programming and we use them in inheritance. -inal &tatic Abstract &trictfp $ative &ynchroni.ed Transient /olatile Final NonAccess Modifier in Java Programming: %hen we use inheritance in Java programming so there is a problem sometimes we don!t need overridden in subclass. And we use -inal non*access modifier to chec" the e0tend feature of the class. %hen we use the final with a class so the class will not be e0tended class. And when we use the final with a primitive data type so it will not able to change its value. !tatic NonAccess Modifier in Java Programming: %e use static modifier for variables and also for methods. %e 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 NonAccess Modifier in Java Programming: %e usually use the abstract on class and also on methods. %hen 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 synta0 of the abstract modifier the abstract class and abstract method should be end with terminator 123. %hen 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. %e should "eep it in our minds during the programming. !trictfp NonAccess Modifier in Java Programming: %e use the &trictfp non*access modifier on with class and methods. %e use it (ust for confirm the standard of I444567 by which it create the floating point independent. And we can!t use the &trictfp in abstract class or methods. Native NonAccess Modifier in Java Programming: %e (ust use the native non*access modifier with methods. And the native methods should end with terminator 123. %hen we use the native with methods it will not implements more. !ync"roni#ed NonAccess Modifier in Java Programming: &ame li"e native non*access modifier we use the synchroni.ed with methods. %hen we used the synchroni.ed with methods they will (ust allow to e0ecute a single thread at a time. %e can use the synchroni.ed with any "ind of access modifier during the (ava programming. $olatile NonAccess Modifier in Java Programming: %e use the volatile on the instance variables. It ma"e the instance variable as volatile its purpose is (ust about the memory. As we "now the variables are used to ta"e a place at memory to "eep data there. The volatile non*access modifier is also used to inform the J/M 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 "now that variables are used to ta"e space in memory to place data over there. Transient NonAccess Modifiers in Java Programming: It is same li"e volatile non*access modifier we use it (ust for instance variables. As we can!t use the instance variables as seriali.ed when they are transient. %e use them commonly in (ava database. The transient "ey word is used to indicate that the instance variable to be s"ip because it is containing seriali.ed ob(ect. %onclusion: In this tutorial we learnt about the modifiers in Java Programming as we discussed two types of modifiers in (ava programming. I.e. access modifier and non*access modifiers and in non*access modifiers we discussed , types. These are all used for different purpose on methods variables and classes. 4very modifier is important in (ava programming and these help us to solve problems during the ideal programming or developing applications. And in listing 8 I used the access modifiers in a simple e0ample. In this tutorial I didn!t use the non*access modifiers. Because these are used in difficult (ava programs. %hich we will use them in my coming tutorials about the (ava programming. 9ead more+ http+''mrbool.com'wor"ing*with*modifiers*in*(ava':;85<=i0..>:[email protected]