Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Private Modifier | Classes Advanced
Java Extended
course content

Contenido del Curso

Java Extended

Java Extended

1. Deep Java Structure
2. Methods
3. String Advanced
4. Classes
5. Classes Advanced

bookPrivate Modifier

How to hide fields and methods from another class?

Access modifiers are used to control the visibility and accessibility of variables and methods in Java. They allow us to define the scope and restrict access to certain members of a class.

We have already discussed access modifiers in the previous section. Now, let's take a closer look at the most commonly used access modifier - private.

As you know, with the private access modifier, we can only access a field/method from the class in which it is located. Let's look at an example of using the private modifier:

For example, if we don't want a variable in one class to be visible or accessible to another class, we can use the private access modifier:

java

Main

copy
123456789101112131415
package com.example; public class Main { public static void main(String[] args) { Person bob = new Person(); bob.name = "Bob"; System.out.println(bob.name); } } class Person { private String name; private int age; private String gender; }

We have declared the fields of the Person class as private. When we try to directly initialize the field, we get an error indicating that the name field has a private access modifier and is not accessible for calling or modifying in the main method.

How to initialize private field?

The simplest way to bypass the protection is to use initialization through a constructor. However, this way, we can only initialize the fields of objects but not access those fields. Let's look at an example:

java

Main

copy
123456789101112131415161718
package com.example; public class Main { public static void main(String[] args) { Person bob = new Person("Bob"); System.out.println(bob.name); } } class Person { private String name; private int age; private String gender; public Person(String name) { this.name = name; } }

We were able to initialize the name field through the constructor, but we still cannot access this field and print it using System.out.println(). To bypass this, we can override the toString() method so that when we call System.out.println(bob);, it displays information about its fields. Let's look at an example:

java

Main

copy
1234567891011121314151617181920212223
package com.example; public class Main { public static void main(String[] args) { Person bob = new Person("Bob"); System.out.println(bob); } } class Person { private String name; private int age; private String gender; public Person(String name) { this.name = name; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + '}'; } }

We finally managed to display the value of the name field of the bob object. We achieved this by using the overridden toString() method, where we implemented the logic for displaying the name.

The question arises, why do we need this access modifier if we have to write so much extra code?

Sometimes, in different classes, there are variables with the same name, and in such cases, it is necessary to hide the variables of these classes so that other classes do not have access to unnecessary fields. This greatly improves the convenience of writing code in general when you understand which field belongs to which class.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 3
some-alt