Private members in java

As we know, private members (either attributes or methods) can only be accessed within the declared class. But, outside the class it is not possible.

Like, if you have a class A and it contains private members, then they can only be accessed or executed within the declared class A. But if you want to try execute them from outside the declared class B, then you will get error. Like - "{attribute} has private access in packag.NameOfYourClass.

Example 1:

Let's imagine, we have a class Student.java in com.company package.

Student.java: contains private members and main method.

Student.java


package com.company; 

public class Student{
  //public members
  private String name = "Mark Smith";
  private int age = 30;
  
  public static void main (String[] args) {
    //access the private members
    Student st = new Student();
    System.out.println(st.name);
    System.out.println(st.age);
  }
}

/**
 * If you now run the example, you will
 * get the following output: 
 * 
 * Mark Smith
 * 30
 */
 

It means, the private members can be accessed smoothly from the decalared class.

Now remove the main method from the Student.java class.

Create another class called Main.java in com.company package. It contains the main method. Now we will try to access private fields of Student.java class from Main.java class. (You will definitely get error)

Student.java class


package com.company; 

public class Student{
  //public members
  private String name = "Mark Smith";
  private int age = 30;
}

Main.java (contains the main method)

public class Main{
  public static void main (String[] args) {
    //try to access the private fields
    //of Students class
    Student student = new Student();
    System.out.println(student.name);
    System.out.println(student.age);
  }
}

If you now run the main.java class, you will get the following error in the console: name has private access in com.company.Student
age has private access in com.company.Student

It means that it is not possible to access the private members (attributes or methods) outside the decalared class.

# How to access the private members from outside the decalared class:

Though we can't access directly private members from outside the decalared class, but it can be done through public getter and setter conventional methods.

Getter: Helps to get or return the attributes value.
Setter: Helps to set the attributes value.

Let's now modify the Student.java and main.java class.

Now Student.java class contains the private members including getter/setter methods.


package com.company;

public class Student{
  private String name; //no need to initialize the variable
  private int age;
  
  // public getter and setter
  public String getName(){
    return name;
  }
  public void setName(String name){
    this.name = name;
  }
  public int getAge(){
    return age;
  }
  public void setAge(String age){
    this.age = age;
  }
}

You see, the private members aren't initialized.

Main.java class (contains main method)


package com.company;

public class Main{
  public static void main (String[] args) {
    //create an object of Student class
    
    Student student = new Student();
    //set the value using setter method
    student.setName("Mark Smith");
    student.setAge(30);
    
    //access the value now using getter
    System.out.println(student.getName());
    System.out.println(student.getAge());
  }
}

Now run the Main.java class. You will get the following output:


Mark Smith 
30

What's going on in the Main.java class? We use setter method to set the attributes value (name and age) and use getter method to access the attributes value.

Hope, you can now realize how to access private members from outside the declared class.