Issue
i have a variable which holds instance of class var a=Objj(); now after doing something i have to reinitiate the variable a with new object like this a=Objj(); then later a=Objj();
is it a good practice? what happens to the older object?
Solution
In your case the older object will be replaced with a new instance. What I would suggest is creating a clear()
method that will reset the data in the instance. Otherwise your original suggestion will work depending on what it is you want to do exactly.
You can safely replace the old instance with a new instance and the dart garbage collector will de-allocate the memory of the variable.
Alternatively, if the constructor is a const
constructor and it is initialized using the const
keyword then at compile time if the objects are identical they will share a single instance at runtime.
var a = const ImmutablePoint(1, 1);
var b = const ImmutablePoint(1, 1);
assert(identical(a, b)); // They are the same instance!
Answered By - Hadi Hassan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.