Akshay Pethani Akshay Pethani - 7 months ago 18
Java Question

What is way to check weather object of class is assigned an instance of class or not in Java?

I've put just small snippet of large code to explain my question:

class A
B b= new B();
B z;
void xyz(){
if(// condition to check z is not assigned instance of class B){
z= new B();

class B{
//variables and methods

I want an Expression in if block of xyz() to check weather object z have assigned an instance of B or not where I've made comment in if block's bracket.
or in other way.

what is a way to check weather any java object have instance of their class or not?


Using instanceof you can check this

Try this:

        B b= new B();
        B z = null; 
        if( z instanceof B){
        } else{
           z = new B();