Uninstalling Java on macOS. How to get your Facebook newsfeed news back. Upcycling an old iPhone 5 and a bluetooth speaker for a tabletop media station. How to easily set up your Mac to empty the trash at timed intervals. Igor Kromin Consultant. Notice: I'm taking a break from blogging to focus on Atari Gamer.
How to get a null return from a Java constructor 4-Sep NOTE: This article is 3 years or older so its information may no longer be relevant. Read on at your own discretion! Can constructor return a null object?
Ask Question. Asked 9 years, 7 months ago. Active 4 months ago. Viewed 49k times. Improve this question. Andrew T. Jonathan Pitre Jonathan Pitre 2, 3 3 gold badges 21 21 silver badges 36 36 bronze badges.
Not relevant. The question is very simple: can a constructor return null? You don't need to know what it is used for.
The OP asked "why this is there". Do you have an answer for that question? AlexLockwood The answer, as everyone below gently put it, is that this is there for no real reason, as what it does is completely useless. There is no reason since o can never be null, and that's what I wanted to know. That's why I asked "what is this code for Add a comment.
Active Oldest Votes. Improve this answer. Lii So no, it can never return null. Jon Skeet Jon Skeet 1. I know it sounds crazy..
Java, Android — Bertram Gilfoyle. AhamadAnees: That sounds very unlikely to me. If you still think it's the case, I suggest you post a minimal reproducible example as a new question.
Thank you for the quick responds. But unfortunately I can't post new questions here. AhamadAnees: Well adding comments instead isn't going to help. As I say, I think it's far more likely that you've misdiagnosed what's going on than that Java on Android is actually behaving that way Or maybe you should improve your old questions so that you can post new questions here We have taken over some.
NET 1. We are continually encountering logic and design decisions that is puzzling us to no end. Here is one example, where the message lsMessage has been retrieved from the queue and ready for processing.
Please ignore the use of label and goto for the moment; that is not the question. Our bewilderment is the check whether the Thread object is null right after instantiation. The else statement below seems to suggest the previous developers have encountered situations like this before.
Of course, the original developers are long gone. So we would like to know, can the CLR really instantiate an object after the call to the constructor and return a null? We have no knowledge of such a possibility.
In my opinion, what the else statement suggests is that the previous developers didn't know their C. A constructor always returns a constructed object or throws an exception. Edit: for clarification there is an insane edge case where you can get null from a class constructor, but frankly I don't think any real code should ever expect to deal with this level of crazy: What's the strangest corner case you've seen in C or. To all normal intents : it won't happen.
No, you can't get null from a class constructor Thread is a class. Other than that, a constructor will always either return an object or initialize a struct , or throw an exception. The only thing is you should check the documentation to see if the constructor can throw any exception.
As core mentions, operator overloading can make it appear that a constructor returned null , when that's not what really happened. The authors of the article core found say they haven't seen it used, but it actually is used in a very popular product: Unity. Now, the fault here is mine, because one should not call the AudioSource constructor directly. Here's what the Unity manual says about UnityEngine. Instatiating a GameObject adds it to the scene so it's completely initialized!
Instantiating a simple UnityEngine. Object has no such semantics, so the sic it stays in the 'destroyed' state which compares true to null. While instantiating a GameObject initializes it, instantiating an AudioSource object doesn't, so the comparison with null returns true. This unusual idiom is made even more stealthy by virtue of the fact that attempts to reference properties of the uninitialized AudioSource object will throw null-reference exceptions, which I initially misinterpreted as meaning the object reference was null , not the property.
Others have answered the OP's question, but I wanted to add this answer because the OP's code might actually make sense if the Thread class therein isn't the one we would expect it to be, just as Object and its descendants isn't quite what you might expect it to be in a Unity script that is, it is actually UnityEngine.
0コメント