Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7806743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:37:52+00:00 2026-06-02T02:37:52+00:00

I would like to clarify something. With plain Java reflection techniques (without using a

  • 0

I would like to clarify something.

With “plain” Java reflection techniques (without using a library) afaik it is not possible to get a reference to a private field (I mean the java.lang.reflect.Field object, no the field value).

For example, if I have this class:

public class MyClass {
  private String field1;
}

If I attempt to execute this:

Field field = MyClass.class.getField("field1");

I will get a NoSuchFieldException exception, as expected.

With the Guava Reflection library, if I try to execute this:

Object o = new MyClass();
Property property = Properties.getPropertyByName(o, "field1");
Field f = property.getField();

I get the following exception:

java.lang.IllegalStateException: Unknown property: field1 in class MyClass

And this was also expected. However, if I add a getter method, like this:

public class MyClass {
  private String field1;
  public String getField1() {return field1;}
}

Then the Guava-reflection code is working.
I have to confess I am a bit loss about this. I understand that a reflection library could use a getter to return the value of a private instance variable, but the Field object itself just because a getter exists ?. Does someone has an idea how does this happen ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:37:54+00:00Added an answer on June 2, 2026 at 2:37 am

    You can reflect on private fields using standard java reflection, which is probably what Guava is doing under the hood:

    Class<?> c = ... some class ...
    Field field = c.getDeclaredField("name");
    field.setAccessible(true);
    Object value = field.get(object);
    

    getDeclaredField allows you to obtain private fields.
    setAccessible is needed to prevent security issues.

    Anyway, as a best practice, consider using reflection on public members only, so work with getters/setters if possible.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to clarify something about TimerTask. When you have the code below:
I would just like to clarify that by 'design', I mean software design, not
I would like to sort an array in ascending order using C/C++ . The
Before you start reading I would like to clarify: I have already thought of
I am using jQuery Ui Autocomplete 1.8.14 and I would like to do two
I currently have a small Java program which I would like to run both
I would like to clarify what is the proper way to filter user input
I have some questions regarding MVC that I would like to clarify. At our

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.