In Java what happens when an object fails to be instantiated?
I come from a c++ background and I find myself constantly doing this in java:
SomeClass sc=new SomeClass();
if(null!=sc)
{
sc.doSomething();
}
What I want to know is what will be in the variable sc if the constructor fails for some reaso开发者_开发知识库n (like maybe not enough memory). I can' t find a straight answer, and I am worried that I am just wasting my time because maybe if the new operator fails would the program just crash anyway?
The Java Specification Language 3rd Edition covers your question thoroughly:
12.5 Creation of New Class Instances
Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden. If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an
OutOfMemoryError
. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values.Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure: [...]
So it's simply not possible for a new
expression to return null
. Whatever is returned, if the execution completes normally, will always be a valid instanceof
whatever class was instantiated.
Handling exceptions
Generally speaking, possible exceptions are usually handled with a try-catch
block:
String someString = askFromUser();
try {
int num = Integer.parseInt(someString);
doSomethingWith(num);
} catch (NumberFormatException e) {
complainAboutIt();
}
In your case, you may consider putting new SomeClass()
in a try
block with a corresponding catch (OutOfMemoryError e)
, but this is highly atypical. Unless you plan to do something meaningful when this happens, in most typical scenarios it's best to not catch
any Error
that may occur during your program execution.
From the documentation:
An
Error
is a subclass ofThrowable
that indicates serious problems that a reasonable application should nottry
tocatch
. Most such errors are abnormal conditions.A method is not required to declare in its
throws
clause any subclasses ofError
that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.
Related questions
- Catching java.lang.OutOfMemoryError
- When to catch java.lang.Error?
- Is it possible to catch out of memory exception in java?
- Can the JVM recover from an OutOfMemoryError without a restart
- How to handle OutOfMemoryError in Java?
See also
- Java Tutorials/Exceptions
If the constructor failed, then it would throw an exception or an error and you wouldn't get any further in the program. Specifically, if it ran out of memory, you would get an OutOfMemoryError
.
SomeClass sc=new SomeClass();
if(null!=sc) { sc.doSomething(); }
An object reference is never null
after creating an object, so the null check is unnecessary because the condition will always be true.
If, for example, there is not enough memory at the point in the constructor, an OutOfMemoryError
will be thrown and the constructor will not return normally.
Yes you are wasting your time :-). sc is guaranteed to be non-null after the constructor is called. If the constructor failed an exception would be thrown and the following code would never be run. Hence the following is safe in Java:
SomeClass sc = new SomeClass();
sc.doSomething();
without throwing a NullPointerException.
That if
check is not needed, and some IDEs will even complain that it is unnecessary as it always evaluates to true
. The only reason that new
would fail is if the constructor threw an exception, and in that case your if
check will be bypassed anyways.
An OutOfMemoryError is thrown which will usually crash the virtual machine. (Could be catched as Throwable)
You might find Prevent and Recover from Java OutOfMemory Error useful.
精彩评论