0

I want to access the car object instance in the Test class from another class

class Test {

private car = new car(12);

}

class car {

     int i;

  car(int i) {
      this.i = i;
  }

}
3
  • Do you mean accessing private fields of a class? Commented Mar 9, 2011 at 22:41
  • Yes, but field type is object, may be Car anything Commented Mar 9, 2011 at 22:44
  • Are you looking for setAccessible? I strongly suggest avoiding reflection. Commented Mar 10, 2011 at 0:10

5 Answers 5

2

Seeing your codes has errors (e.g class car must be Car), I would suggest reading tutorials such as this one and this one, on how to retrieve fields using reflection.

Here's a quick example based on your comment:

public class Test{

private car = new car(12); 

    public Test() {
        Fields[] fields = this.getClass().getDeclaredFields();
        if (fields != null) {
            for (Field field: fields) {
                Class<?> fieldType = field.getType();
                System.out.println(fieldType.getName());

                if (Car.class == fieldType) {
                    System.out.println("Field: " + field.getName() + " is of type " + Car.class.getName());
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I know how to retrive fiels types such as long, int. but stuck i stuck in the object type
Is there any method field.getValue() .? Yes I want to get the object
Yes Car car = (Car)field.get(this);. I hope you read the documentation I posted here.
1

To access a private attribute of a class you need to make a method to return that attribute / field. so to return the car of Test write public Car getCar(){ return car; }

1 Comment

I want to try this via reflection
0

For tests that really must access private members, see if you can set the private members to protected, and then have the Test (or a testing wrapper) subclass the "to be tested" object. That said, it's better to test 100% through method calls.

Testing is supposed to allow for encapsulation, so you really want to get out of the business of looking at an object's internals. If you break encapsulation in your testing framework, you lose all of the possible maintainability benefits of having encapsulation. In plain words, if you ever try to "fix" your object, you'll have to rewrite all of your tests to comply with the new "fixed" architecture.

It is far, far better to just test the part of the Object that the rest of the world will see, which is the outside after you've read / set / processed whatever is required. That way, if you ever change the internal guts, you will have a suite of tests already "ready to go" to verify your changes didn't break the object's behavior as viewed from the rest of the world.

2 Comments

I want to do this through reflection api. Is that possible
Yes, it is possible, but it's a really bad idea. So bad that your tests will have little re-usability, in which case it's a wonder why you are investing in writing such a throw away test in the first place.
0
Test test = new Test();
Field f = test.getClass().getField("car");
Object car = f.get(test);

Comments

0

I wanted to do this to inspect some classes and find their default internal state after the new operator. Anyway I found the answer on this tutorial:

The whole tutorial series is useful. If you are in a hurry, I've pasted the interesting bit here:

 PrivateObject privateObject = new PrivateObject("The Private Value");

 Field privateStringField = PrivateObject.class.
             getDeclaredField("privateString");

 privateStringField.setAccessible(true);

 String fieldValue = (String) privateStringField.get(privateObject);
 System.out.println("fieldValue = " + fieldValue);

Simple really. Once you find someone with time to explain. Thank goodness for stackoverflow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.