- 0
Notifications Mark All Read
No New notification
- Login
- Get Prime
Java Program to Access Private Members of a Class
What is class in Java ?
In the Java programming language, a class is a blueprint for creating objects. A class defines the properties and behaviors that objects of that class will have. For example, you might define a class called “Dog” that has properties like breed, color, and size, and behaviors like barking and wagging its tail.
Ways that you can access private members of a class :
public class Main { public static void main(String[] args) { MyClass obj = new MyClass(); // access private field using public getter method int fieldValue = obj.getMyField(); System.out.println(fieldValue); } } class MyClass { private int myField; public int getMyField() { return myField; } }
Output :
0
2. Use a public setter method: You can also create a public method in the class that sets the value of the private field. This is called a setter method. For example:
public class Main { public static void main(String[] args) { MyClass obj = new MyClass(); // set value of private field using public setter method obj.setMyField(123); } } class MyClass { private int myField; public void setMyField(int value) { myField = value; } }
Output :
...Program finished with exit code 0 Press ENTER to exit console.
3. Use reflection: Another option is to use the Java Reflection API to access the private field or method. Reflection allows you to inspect and manipulate the class at runtime, including accessing private fields and methods. However, this approach is generally discouraged because it can be fragile and can break encapsulation, which is an important principle of OOP.
import java.lang.reflect.Field; public class Main { public static void main(String[] args) throws Exception { MyClass obj = new MyClass(); // use reflection to access private field Field field = MyClass.class.getDeclaredField("myField"); field.setAccessible(true); int fieldValue = field.getInt(obj); System.out.println(fieldValue); } } class MyClass { private int myField; }
Output :
0
4. Make the field or method protected: Another option is to change the visibility of the field or method to protected. A protected field or method is accessible from within the class and from any subclass of the class, but not from other classes in the same package.
public class Main { public static void main(String[] args) { MyClass obj = new MyClass(); // access protected field from main class int fieldValue = obj.myField; System.out.println(fieldValue); } } class MyClass { protected int myField; }
Output :
0
5. Make the field or method package-private: Finally, you can make the field or method package-private, which means it is only accessible from within the package. To do this, you simply omit the public, private, or protected access modifier. Package-private members are accessible from any class in the same package, but not from classes in other packages.
public class Main { public static void main(String[] args) { MyClass obj = new MyClass(); // access package-private field from main class int fieldValue = obj.myField; System.out.println(fieldValue); } } class MyClass { int myField; }
Output :
0
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment