Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, September 15, 2016

How to reset current object in Java?

How to reset current object in Java?


If I have an object myObject of type Foo, while inside myObject, is there a way to reset itself and run the constructor again?

I know the following does not work, but might help convey the idea.

this = new Foo();  

Answer by talnicolas for How to reset current object in Java?


Just write a method that will "reset" all the variables of your object (null or 0 or default value).

Answer by dasblinkenlight for How to reset current object in Java?


There is no way to run a constructor again on an existing instance. However, you can organize your code in a way to allow resetting with a minimum amount of work, like this:

public class MyClass {      public MyClass() {          reset();      }      public void reset() {          // Setup the instance          this.field1 = ...          this.field2 = ...      }  }  

Note: your reset method needs to set all fields, not just the ones that you usually set in the constructor. For example, your constructor can rely upon the default initialization of reference fields to null and numeric fields to zero; your reset method needs to set them all explicitly.

Answer by RhinoFeeder for How to reset current object in Java?


Have a set of default values or states for your class stored inside of it. Then write a reset() method that will restore all of these defaults within the class.

public void reset(){      // Reset everything to your default values  }  

Answer by Robin Green for How to reset current object in Java?


An alternative approach, which is gaining in popularity because it tends to make code easier to reason about, is to make your objects immutable, and instead of changing their state (e.g. resetting them), simply create a new object.

Answer by Mohamed Abdullah J for How to reset current object in Java?


It is better to set null or use new Object()

obj = null;     or  obj = new Object();  

The garbage collector will clear the object finally

Answer by Arun Sudhakaran for How to reset current object in Java?


@SuppressWarnings({ "unchecked" })  static void emptyObject(Object obj)   {       Class c1 = obj.getClass();       Field[] fields = c1.getDeclaredFields();         for(Field field : fields)       {            try            {                 if(field.getType().getCanonicalName() == "boolean")                 {                      field.set(obj, false);                 }                 else if(field.getType().getCanonicalName() == "char")                 {                      field.set(obj, '\u0000');                 }                 else if((field.getType().isPrimitive()))                 {                      field.set(obj, 0);                 }                 else                 {                      field.set(obj, null);                 }            }            catch(Exception ex)            {              }       }}  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.