Hi,
this is my code:
Code:protected void method1(){ ... if (condition1==true){ MyObjectClass myObject1=new MyObjectClass(); MyObjectClass myObject2=new MyObjectClass(); } ... } protected void method2(){ ... If (condition1==true){ //Do some operations with myObject1 and myObject2; } ... }
The problem is that myObject1 and myObject2 are not accessible from method2. I Think I can't use singleton, because I need more than one instance of MyObjectClass and I neither can't use this:
because I don't need to create these two objects unless condition1==true.Code:class GlobalVariables{ MyObjectClass myObject1=new MyObjectClass(); MyObjectClass myObject2=new MyObjectClass(); }
Thank you for advice.
Declare them in the class but don't instantiate them unless your condition1 is true.
Code:MyObjectClass myObject1; MyObjectClass myObject2; protected void method1(){ ... if (condition1==true){ myObject1=new MyObjectClass(); myObject2=new MyObjectClass(); } ... } protected void method2(){ ... If (condition1==true){ //Do some operations with myObject1 and myObject2; } ... }
I had no clue it is so simple. :-)
Thank you, guys for your response.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks