user868100
user868100

Reputation:

Java: Is it possible to get the default value of a field without instantation?

I have a Java class that I cannot instantiate directly. I need to know what is the default value of one of its fields.

e.g.

Class Foo {
  private Bar field = new Bar("baz"); // can I know that field 
                                      // is set to new Bar("baz")?
}

Upvotes: 3

Views: 4246

Answers (6)

Roger Lindsjö
Roger Lindsjö

Reputation: 11543

With some (ok, a lot) of work you could parse the byte code your self. For simple classes is should not bee too hard, but imagine a class such as this:

public class Foo {
    private Bar field = OtherClass.someStaticMethod();
}

then it would be much harder.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533520

The details of how a field is initialised is in the constructor. You can call a constructor using reflections if you have to (with setAccessible(true)) If you can't call it for some reason you can read the byte-code of the class to determine what each field would be initialised to. (This is effectively the same as calling the constructor but you have complete control)

Upvotes: 1

Stephen C
Stephen C

Reputation: 718798

If you can't instantiate it, you can't get the value of one of its fields.

In fact, when you think about it, this is totally nonsensical. The value of the field (i.e. the object reference to a specific Bar object) does not exist until the Bar object has been created. And that only happens when the Foo object is instantiated.

This would make more sense if field was initialized to a Java constant expression, but you can't do that with a reference type (apart from String).


The only possible wiggle room here is that you might be able to instantiate the Foo object indirectly or by using reflection and setAccessible to break the Java visibility restrictions. But even then, you are getting the field value of a specific Foo object that you have instantiated ... not the (nonsensical) field value of an object that doesn't exist yet.

Upvotes: 1

ApprenticeHacker
ApprenticeHacker

Reputation: 22011

You may be able to achieve this using the awesome yet scary hack described by @glowcoder. However, I don't recommend it, for the sake of Java and whoever will end up maintaining your code.

I suggest you some method to check for the default value.(the old getter/setter) Anyway, I fail to see why you would not want to do instantiation. Is there some special reason?

Upvotes: 0

clankfan1
clankfan1

Reputation: 225

If I understood correctly you could could create a method to return the value of that field like this:

public Bar getBar() {
     return field;
}

Then whenever you use the method getBar(); in your code it will return the current value of the field, field. (You should probably change the name of field)

So to get the default value just run it before you set your field.

Upvotes: -1

corsiKa
corsiKa

Reputation: 82579

Simply put, you can't.

But at the same time, you can with some really horrible hacks.

You could use reflection to create the object, and then change the object's state to public so you can access it, and then get the value for it. This is really bad to do, but I suppose it's possible. I don't have code ready, but I'm sure there's a billion code samples on the internet for instantiating an object using reflection.

Upvotes: 1

Related Questions