Java

What is private Keyword in Java

In Java, the term “private” serves as an access modifier. Variables, methods, constructors, and inner classes can all utilize it.

Note:

  • Comparing the private modifier to others like public, default, and protected, it is the most limiting.
    It cannot be used on an interface or a class (other than an inner class).
  • The class is the only place from which the private variables and methods may be accessible. No other class is allowed to use these.
  • A class’s objects cannot be produced outside of the class if it has a private constructor.

Java private variable Example

Because we are attempting to access a private variable outside of the class, this application returns an exception.

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:

Private variable example output

Java private method Example

Let’s check to verify if a private method may be accessed outside of the class where it was 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 method example output

Java private class Example

In this instance, a class has been made secret. As you can see, there was a compilation problem and the application did not run correctly. This is due to the fact that no class (apart from an inner class) can contain 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:

private class Example output

Java private constructor Example

We have a class called MyClass here, and its default constructor is private. We’re attempting to make an instance of this class in the JavaExample class. Because you cannot create an object of a class with a private constructor outside the class, this caused a compilation problem.

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:

private constructor example output

How to access private fields of class outside the class

Getter and setter techniques are employed.

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

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button