The private keyword is an access modifier in java. It can be used for variables, methods, constructors and inner classes.
Note:
- The private is the most restrictive modifier compared to other modifiers such as public, default and protected.
- It cannot be applied to a class (except inner class) or an interface.
- The private variables and methods can only be accessed within the class. No other class can access these.
- If a class has private constructor, then the object of this class cannot be created outside this class.
Java private variable Example
This program throws an error because we are trying to access a private variable outside the class.
class MyClass { private String str="I am a private variable"; } public class JavaExample { public static void main(String[] args) { MyClass obj = new MyClass(); System.out.println(obj.str); } }
Output:
Java private method Example
Let’s see, if we can access a private method outside the class in which it is declared.
class MyClass { private void demo() { System.out.println("I am a private method of class MyClass"); } } public class JavaExample { public static void main(String[] args) { MyClass obj = new MyClass(); obj.demo(); } }
Output:
Java private class Example
In this example, we have declared a class private. As you can see that the program didn’t run fine and threw a compilation error. This is because a class (except an inner class) cannot have a private access specifier.
private class JavaExample { void demo(){ System.out.println("Hello"); } public static void main(String[] args) { JavaExample obj = new JavaExample(); obj.demo(); } }
Output:
Java private constructor Example
Here, we have a class MyClass
that has a private default constructor. We are trying to create an object of this class in another class JavaExample
. This threw a compilation error because you cannot create an object of a class with private constructor, outside the class.
class MyClass{ //private constructor private MyClass(){ System.out.println("Private constructor of MyClass"); } } public class JavaExample { public static void main(String[] args) { //creating an object of MyClass MyClass obj = new MyClass(); } }
Output:
How to access private fields of class outside the class
We use getter and setter methods.
class Teacher { private String designation = "Teacher"; private String collegeName = "Beginnersbook"; public String getDesignation() { return designation; } protected void setDesignation(String designation) { this.designation = designation; } protected String getCollegeName() { return collegeName; } protected void setCollegeName(String collegeName) { this.collegeName = collegeName; } void does(){ System.out.println("Teaching"); } } public class JavaExample extends Teacher{ String mainSubject = "Physics"; public static void main(String args[]){ JavaExample obj = new JavaExample(); /* Note: we are not accessing the data members * directly we are using public getter method * to access the private members of parent class */ obj.setCollegeName("My College"); obj.setDesignation("My Teacher"); System.out.println(obj.getCollegeName()); System.out.println(obj.getDesignation()); System.out.println(obj.mainSubject); obj.does(); } }
Output:
My College My Teacher Physics Teaching