How do you check if classinstance has been created/initialised or is null?
private MyClass myclass;
if(!check class has been initialised) //<- What would that check be?
myclass = new MyClass();
Thanks in advance
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just check if it is null
As mentioned in your comment,
will not work because if myclass is null, that’s translated to
which leads to a
NullReferenceException.By the way, objects declared in class scope are automatically initialized to null by the compiler, so your line:
is the same as
Objects declared in a method are forced to be assigned some initial value (even if that initial value is null).