How could I access a private object from another class?

AMDG

Is there some direct or indirect way to access a private object from another class?

Makoto

Well, reflection is really your only hope on this one. It'd be the only way you could inspect an object's fields without caring much for its visibility modifiers.

Entity entity = new Entity();
for(Field f : entity.getClass().getDeclaredFields()) {
    try {
        f.setAccessible(true);
        System.out.println(f.get(entity));
    } catch(IllegalAccessException e) {
        e.printStackTrace();
    }
}

But, I do want to strongly caveat you on resetting the accessibility of the field.

If you're running in a highly concurrent environment, and you disable the accessibility immediately after you're done, you will run into a case in which the accessibility is removed in one thread, as you're about to read it in another, which will lead to a giant, confusing mess.

At that point, the fact that you have visibility modifiers becomes moot, and you would want to just use a part-and-parcel getter instead (or have the field be public, which makes many feel weird about).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How could I access a private object from another class?

From Dev

Access an object from another class in private method

From Dev

How do I access the private lists from another class created by netbeans when i bind elements

From Dev

How to access an object from another class

From Java

Access private field of another object in same class

From Dev

How to access variable from another class directly(i.e. without using class name or object)?

From Dev

Could I overload operator= to assign object of a class to a variable of another class but both class are from the same class template

From Dev

How do I access a private variable from a class?

From Dev

How to access an object of another class

From Dev

How could a class inherit from another class?

From Dev

How do i get a class member function to have access to another class member function's private member?

From Dev

How can I access an object defined in another class?

From Dev

How to Access Variable from Another Class without Creating New Object

From Dev

how to access an object of classA defined in main from another class

From Dev

Why can I use a collection initializer with private set access from another class?

From Dev

How to access private fields from parent class

From Dev

How to access private fields from parent class

From Dev

How do i access a specific object from with in the class it was created from

From Dev

How could I access an object value

From Dev

How to access private function of a class in another class in c#?

From Dev

How do I access an enum from another class in Swift?

From Dev

How would I access variables from one class to another?

From Dev

How do I gain access to an objects variable from another class?

From Dev

How do I access an enum from another class in Swift?

From Dev

How can I access to an ArrayList from another class?

From Dev

How do I access a list in a class from within another list

From Dev

How do I access ListIterator.previous() from another class

From Dev

How can i access an object from another method in java?

From Dev

How can I access the value of a private attribute in an abstract class in a subclass object?

Related Related

  1. 1

    How could I access a private object from another class?

  2. 2

    Access an object from another class in private method

  3. 3

    How do I access the private lists from another class created by netbeans when i bind elements

  4. 4

    How to access an object from another class

  5. 5

    Access private field of another object in same class

  6. 6

    How to access variable from another class directly(i.e. without using class name or object)?

  7. 7

    Could I overload operator= to assign object of a class to a variable of another class but both class are from the same class template

  8. 8

    How do I access a private variable from a class?

  9. 9

    How to access an object of another class

  10. 10

    How could a class inherit from another class?

  11. 11

    How do i get a class member function to have access to another class member function's private member?

  12. 12

    How can I access an object defined in another class?

  13. 13

    How to Access Variable from Another Class without Creating New Object

  14. 14

    how to access an object of classA defined in main from another class

  15. 15

    Why can I use a collection initializer with private set access from another class?

  16. 16

    How to access private fields from parent class

  17. 17

    How to access private fields from parent class

  18. 18

    How do i access a specific object from with in the class it was created from

  19. 19

    How could I access an object value

  20. 20

    How to access private function of a class in another class in c#?

  21. 21

    How do I access an enum from another class in Swift?

  22. 22

    How would I access variables from one class to another?

  23. 23

    How do I gain access to an objects variable from another class?

  24. 24

    How do I access an enum from another class in Swift?

  25. 25

    How can I access to an ArrayList from another class?

  26. 26

    How do I access a list in a class from within another list

  27. 27

    How do I access ListIterator.previous() from another class

  28. 28

    How can i access an object from another method in java?

  29. 29

    How can I access the value of a private attribute in an abstract class in a subclass object?

HotTag

Archive