0

I'm interested to know that, is it really possible to create an object reference for a java class without invoking any of it's constructors? If yes, how?

7
  • 1
    No, it's not possible. Even when you create the instance via reflection, the default constructor is invoked. Commented Sep 10, 2014 at 20:15
  • Object myObject = null; --> IS this what you would be looking for? Commented Sep 10, 2014 at 20:16
  • It might be possible with sun.misc.Unsafe. But in "normal" Java, it's not possible. Commented Sep 10, 2014 at 20:16
  • "an object reference for a java class" - the object should refer to the class or to be an instance of the class ? Commented Sep 10, 2014 at 20:18
  • 1
    @Eran It is possible by using JNI. Check my answer for a reference. Commented Sep 10, 2014 at 20:42

4 Answers 4

4

Definitely a bad idea, but you could use the sun.misc.Unsafe class to allocate an instance:

public static class TestClass {
    int field = 1;
}

public static void main(String[] args) throws Exception {
    Constructor<Unsafe> constructor = Unsafe.class.getDeclaredConstructor();
    constructor.setAccessible(true);
    Unsafe unsafe = constructor.newInstance();

    TestClass test = (TestClass) unsafe.allocateInstance(TestClass.class);
    System.out.println(test.field);
    System.out.println(new TestClass().field);
}

Output:

0
1
Sign up to request clarification or add additional context in comments.

Comments

3

It is possible by using JNI.

http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html

Object Operations

AllocObject

jobject AllocObject(JNIEnv *env, jclass clazz);

Allocates a new Java object without invoking any of the constructors for the object. Returns a reference to the object.

The clazz argument must not refer to an array class.


It is hard to find any relevant usage of it. I would not use it but it is still an answer to the question.

Comments

1

The only way i can think of is using the clone() method

Object objectA = objectB.clone();

Now objectA.clone() != objectA and objectA and objectB point to two different places in memory so technically you create an object without calling its constructor.

1 Comment

What if the class does not implement Cloneable ?
0

Not possible. Every class has a default constructor that calls its parent constructor super() which boils down to Java's Object Class.

4 Comments

Not true you can override default constructor to be private
Yes but there is always a constructor!
Can't create an object without calling some constructor either (default called implicitly) or your own created constructor called explicitly.
Technically, it is possible, obviously not through reflection. Still there's no real need to create an object by not using a constructor.

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.