Katiyman
Katiyman

Reputation: 846

How to know if the bean has got some values or not

Suppose there is some bean named employee:

and has many fields like first name, last name, emp no, age , addr, designation etc.

Is there a way by which I can test if none of the fields have been set?.

I mean, I get a reference through which I can get all values one by one and check if all are empty/null. but is there a better way to find if none of the fields have been set in the bean

Upvotes: 0

Views: 205

Answers (1)

Yes, you can do it exactly like you are thinking... there is no Out of the Box method for that, but you can create a method that return a boolean and this represent the state of the Bean you want to check...

Example:

public boolean isProperlyInitialized(){
     boolean condition1 = age!=0;
     boolean condition2 = ...;

     return condition1 && condition2 &&....;
}

and implemented in the code

Bean b = new Bean();

if(b.isProperlyInitialized){
    //Do something cool
}

Upvotes: 2

Related Questions