Core JAVA
Java Runtime Environment (JRE)
Java Virtual Machine (JVM)
Java overview
Java basics
Java Objects and classes
Java Constructors
Java basic datatypes
Java variable types
Java modifiers/Access Modifiers In Java
Java Basic Operators
Java Loops and Controls
Java conditions
Java numbers and characters
Java strings
Java arrays
Java date time
Java methods
Java file and IO operations
Java exceptions
Inner class
Java OOPs Concepts
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java Interface
Cohesion and Coupling
Association, Aggregation and Composition
Java Collections
Java ArrayList
Java LinkedList
Set and HashSet
LinkedHashSet and TreeSet
Queue and PriorityQueue
Deque and PriorityQueue
Java Map Interface
Java HashMap
Internal Working Of Java HashMap
Java Mutithread
Methods of Thread In Java
Join , run & Start Method in Threads
Difference b/w start & run Methods in Threads
Java Concurrency Package & its Features
CountDownLatch, CyclicBarrier, Semaphore and Mutex in Thread
Access Modifiers In Java
Access modifiers in java are reserved keywords in java programming languages which are used with classes, variables, methods and constructors to define their scope, for example a class with public key word(modifier) means it is available to be used in the entire java project.
We will see more examples and types of access modifiers in java in the article below.
Types of Access Modifiers in Java
There are two types of modifiers in java:
- Java Access Modifiers
- Non Access Modifiers
Java Access Modifiers
Java access modifiers are used to define access level or scope of any variable, class or method. We can use any of the below before naming our variables, classes or method to limit their scope or accessibility.
- Default modifiers – These do not need any keyword, if you don’t specify a specific modifier variable or class will fall under this which means visible to package level.
Any code within the package may access it. - Private modifiers – Only visible to the class in which it is used, this is very restrictive and should be used if scoped is really within a class, for example a variable or a method which is needed in the same class to support another method or code, we may make them private.
Keyword used: private
- Public modifiers – Public modifiers are visible to the entire project or JVM, it is used a lot because most of the time we want all our methods and classes accessible across project or JVM.
Keyword used: public - Protected modifiers – Protected modifiers are visible within the package to all sub classes and classes.
Keyword used: protected
Example:
// class with public access modifier public class Garden{ void printDetailsOfGarden(int floweNumber) { System.out.println("I am a garden class, total flowers are:"+floweNumber); } //Method with private modifier private int getNoOfFlowers() { return 30; } public static void main(String args[]) { Garden garden = new Garden(); garden.printDetailsOfGarden(garden.getNoOfFlowers()); } // A inner class Plants with private modifier private class Plants{ } }
Non-Access Modifiers
There are some key words which are widely used in java for different purposes, we may call them non-access modifiers, we may see more details in coming chapters.
- Static modifier – static keyword is with variables, methods in class to make them statically accessible which means we can directly access them with  <class name>.<name of variable/method>
- Final modifier – final keyword is used with class, variables and methods which ensures once they are defined nothing in code changes them, for example a int variable declared with final keyword will stop any change in its value later from anywhere in the entire project.
- Abstract modifier – abstract this keyword is used to create abstract classes, we will see more details about abstract in the coming chapter, it helps in achieving OOPs concept abstraction.
- Synchronized and volatile – synchronized and volatile keywords are used in java multi threading, we will see more details about this topic in coming chapters.
Example:
// class with public access modifier public class Garden{ //final and static in used, please not such declarations will have variable names in caps letters final static int GARDEN_AGE = 30 ; void printDetailsOfGarden() { System.out.println("I am a garden, I am :"+GARDEN_AGE+" years old."); } //main method with static key word public static void main(String args[]) { Garden garden = new Garden(); garden.printDetailsOfGarden(); } // A inner class Plants with abstract modifier abstract class Plants{ //an abstract method abstract void plantDetails(); } }
References
Access modifiers by oracle :Â Access Modifiers
Next Article To Read
Previous article, if you missed to read:Â Java Variable Types
Excellent article on java access modifiers, thanks for keeping it simple.