Is there any way to make the constructor return a = = null object in Java?

I have a feeling that the answer is "no", but I think I'll ask just in case

Pseudo code:

Foo foo = new Foo();

if(foo != null){
    foo.useMe();
}else{
   System.out.println("foo Failed to initialize");
}

What do I need to do in foo to achieve this goal?

Solution

Normally, you only need to throw an exception for this case You can easily write your own exceptions, or just use throw new exception ("foo failed to initialize"); Grab that

Write your own exceptions: http://www.javaplex.com/blog/java-creating-custom-exceptions/

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>