Access Modifiers - AnsuDennis/Java GitHub Wiki
Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:
At the top level—public, or package-private (no explicit modifier). At the member level—public, private, protected, or package-private (no explicit modifier).
default class modifier
- A class is called a Default Class is when there is no access modifier specified on a class.
- Default classes are visible inside the same package only.
- Default access is also called Package access. Example package com.rithus.classmodifiers.defaultaccess.a; // No public before class. So this class has default access*/ class DefaultAccessClass { //Default access is also called package access }
Another Class in Same Package: Has access to default class package com.rithus.classmodifiers.defaultaccess.a; public class AnotherClassInSamePackage { //DefaultAccessClass and AnotherClassInSamePackage //are in same package. //So, DefaultAccessClass is visible. //An instance of the class can be created. DefaultAccessClass defaultAccess; }
Class in Different Package: NO access to default class package com.rithus.classmodifiers.defaultaccess.b; public class ClassInDifferentPackage { //Class DefaultAccessClass and Class ClassInDifferentPackage //are in different packages (*.a and *.b) //So, DefaultAccessClass is not visible to ClassInDifferentPackage //Below line of code will cause compilation error if uncommented //DefaultAccessClass defaultAccess; //COMPILE ERROR!! }
private access modifier a. Private variables and methods can be accessed only in the class they are declared. b. Private variables and methods from SuperClass are NOT available in SubClass.
default or package access modifier a. Default variables and methods can be accessed in the same package Classes. b. Default variables and methods from SuperClass are available only to SubClasses in same package not outside package.
What is protected access modifier? a. Protected variables and methods can be accessed in the same package Classes. b. Protected variables and methods from SuperClass are available to SubClass in any package
Non-Access Modifier
**Final ** Final class cannot be extended. Example of Final class in Java is the String class. Final is used very rarely as it prevents re-use of the class.Consider the class below which is declared as final. Final Class examples : String, Integer, Double and other wrapper classes
Final methods cannot be overridden. Consider the class FinalMemberModifiersExample with method finalMethod which is declared as final.Any SubClass extending above class cannot override the finalMethod
Once initialized, the value of a final variable cannot be changed. Final Variable example : java.lang.Math.PI
Final arguments value cannot be modified. Consider the example below: void testMethod(final int finalArgument){ //final argument cannot be modified //Below line, uncommented, causes compilation Error //finalArgument = 5;//COMPILER ERROR }
Volatile
Volatile can only be applied to instance variables. A volatile variable is one whose value is always written to and read from "main memory". Each thread has its own cache in Java. The volatile variable will not be stored on a Thread cache.
Static
Static variables and methods are class level variables and methods. There is only one copy of the static variable for the entire Class. Each instance of the Class (object) will NOT have a unique copy of a static variable.
- static methods or class level methods can only access static variables no instance variables
- non static methods(member level methods) can have both static and normal variables
- local variables cannot be static