Accessing Private Members using Java Reflection PRO

Using the Java Reflection API we can access private members from outside the scope of the class they belong to. When the private member is a field we can even change its value and when the private member is a method we can even indirectly invoke it.

The AcceesibleObject class is the base class for Method, Field and Constructor. The AccessibleObject class provides the ability to flag a reflected object and suppress default Java language access control checks. Flagging a reflected object is done by calling the setAccessible method that was defined in the AccessibleObject class. The following code shows that.

package com.abelski.samples;

import java.lang.reflect.*;

public class Demo
{
 public static void main(String[] args) throws
      SecurityException, NoSuchFieldException, NoSuchMethodException, 
      IllegalAccessException, InvocationTargetException, IllegalArgumentException
 {
  Book book = new Book();
  System.out.println(book);
  Class ob = book.getClass();
  Field titleField = ob.getDeclaredField(“title”);
  titleField.setAccessible(true);
  Method multiplePagesMethod = ob.getDeclaredMethod(“multiplePages”,int.class);
  multiplePagesMethod.setAccessible(true);
  titleField.set(book, “GoGoMango”);
  multiplePagesMethod.invoke(book,4);
  System.out.println(book);
 }
}

class Book
{
 private String title = “NoNameTitle”;
 private String author = “NoNameAuthor”;
 private int pages = 100;
 private void multiplePages(int num)
 {
  pages*=num;
 }
 public String toString()
 {
  return title+”:”+author+”:”+pages;
 }
}
 

 

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences