0

This is self answered question meant to share some knowledge from http://java.dzone.com/articles/understanding-sunmiscunsafe


I am using Oracle HotSpot Java Virtual Machine.

Suppose I have a class A and I want to create an instance of this class without calling its constructor (lets say that constructor is private, expensive, or simply does things I want to avoid).

For instance how to create instance of this class

class A{
    private A(){
        System.exit(0);//or any task I want to avoid
    }
}

without making my application exit? Is it possible?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Filipe Miranda
  • 914
  • 1
  • 20
  • 33
  • 3
    Why do you want to do this? – Sotirios Delimanolis Jun 28 '15 at 18:23
  • 1
    You can't. By definition, you have to invoke a constructor to construct an instance. – Andy Turner Jun 28 '15 at 18:24
  • Beside constructors and reflection you can use deserialization and `clone`. If it is not what you are looking for then you simply can'd to it. – Pshemo Jun 28 '15 at 18:25
  • I don t see the reason for downvoting, it is a valid question. – Filipe Miranda Jun 28 '15 at 18:27
  • 3
    "And yes there's a way" then you are not telling us something important about your question. There is deserialization which I already mentioned, but I don't know if that is what you want. – Pshemo Jun 28 '15 at 18:28
  • 1
    "And yes there's a way" please, tell us how. – Andy Turner Jun 28 '15 at 18:29
  • Take a look at [What are all the different ways to create an object in Java?](https://stackoverflow.com/questions/95419/what-are-all-the-different-ways-to-create-an-object-in-java). Maybe there you will find informations you are looking for. – Pshemo Jun 28 '15 at 18:32
  • If it is really bad question, I can remove it, but if I can improve it, just tell me. – Filipe Miranda Jun 28 '15 at 18:47
  • One big factor which can make this question better is adding reasonable information about *why do you want to do so* like in which cases it could be useful. Anyway code in your answer is discouraged since `Unsafe` doesn't belong to public API which means it can be changed in different Java versions (even from same provider). – Pshemo Jun 28 '15 at 18:51
  • Shouldn't the question only be answered rather than discussed? Sorry guys, if I did something wrong. I read this: http://java.dzone.com/articles/understanding-sunmiscunsafe Really interesting, just wanted to share the knowledge. – Filipe Miranda Jun 28 '15 at 18:54
  • 1
    It is good that you wanted to share your knowledge. You simply did it wrong way. If you already know an answer when you are creating your question mark "Answer your own question" option so you can post your answer without creating confusion. But remember that question you ask needs to be very clear and answer needs to be correct. In this case your question is quite unclear and looks more like [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) while your answer doesn't respect fact that using classes from `sun` package is discouraged. – Pshemo Jun 28 '15 at 19:01
  • Ok, thanks, I will follow those guidelines. I all put a note about that it is dicourage to use classes from sun. Thanks again. – Filipe Miranda Jun 28 '15 at 19:12
  • I tried to improve clarity of your question by adding example of problem which you may want to avoid. Since I am not native English speaker feel free to improve my edit to correct grammar or other problems you see. – Pshemo Jun 28 '15 at 19:28

3 Answers3

3

Here is how:

Field theUnsafe = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
sun.misc.Unsafe unsafe = (sun.misc.Unsafe) theUnsafe.get(null);
A instance = (A)unsafe.allocateInstance(A.class);
System.out.println("done!");

It is recommended to not use classes from sun package. Which does not mean you can't, so be aware of it.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Filipe Miranda
  • 914
  • 1
  • 20
  • 33
2

There is no way to do it other than invoking a constructor. Even when reflection is used, it invokes the constructor in the background. If there was a way, patterns like the singleton pattern would not be functional.

M A
  • 71,713
  • 13
  • 134
  • 174
1

How do I create an instance without using the constructor?

You don't. The constructor must be invoked to create an instance in Java. But you could make a builder or factory method to invoke it.

// Factory method.
public static A makeIt() {
    return new A();
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249