gameControl = null;
gameControl = new GameControl();
No need to assign null explicitly before assigning a new object.
System.gc();
gameControl = new GameControl();
No seed to call for a garbage-collector, for two reasons.
First of all, this call is merely a request to the garbage-collector to run, not an order. The garbage-collector may or may decide to run, and may or may not complete its work right away (may be interrupted or rescheduled depending on the implementation details of a particular garbage collector).
Secondly, garbage collection does not affect the reassignment of your gameControl
variable that formerly pointed to object A now pointing to object B. The reassignment happens immediately. If no other object points to A, then A becomes a candidate for garbage-collection. So you do not really care if A is collected (meaning cleared from memory) now or later; your app no longer knows about the existence of A.
Example app
Here is an example app. For your convenience, you can cut-and-paste the entire example thing into a single .java
file and execute. In a real app I would use two separate .java
files, one per class.
Note that our example here instantiates a new GameControl
object three times. Each new GameControl
assigns itself a new UUID
as an identifier. That proves you are getting a fresh new GameControl
each time.
Example app
package work.basil.example;
import java.time.Instant;
import java.util.UUID;
/* Name of the class has to be "Main" only if the class is public. */
class ResetExample {
public GameControl gameControl;
// The `main` method.
public static void main ( String[] args ) {
System.out.println( "The main method running at " + Instant.now() );
ResetExample app = new ResetExample(); // Get the app going.
app.gameControl = new GameControl(); // Populate the `gameControl` member field for the first time.
System.out.println( "Current GameControl `id`: " + app.gameControl.id );
app.reset(); // `reset` method replaces the `gameControl` member field’s current object with a new freshly instantiated `GameControl` object.
System.out.println( "Current GameControl `id`: " + app.gameControl.id );
app.reset();
System.out.println( "Current GameControl `id`: " + app.gameControl.id );
}
public void reset () {
System.out.println( "The `reset` method is running at: " + Instant.now() );
this.gameControl = new GameControl();
}
}
class GameControl {
// Member fields
public UUID id;
// Constructor
public GameControl () {
System.out.println( "The constructor of `GameControl` is running at: " + Instant.now() );
this.id = UUID.randomUUID();
}
}
When run.
The main method running at 2019-02-09T02:33:16.233414Z
The constructor of GameControl
is running at: 2019-02-09T02:33:16.265839Z
Current GameControl id
: 4ee963c6-a895-4fe8-8463-b890777ad8f4
The reset
method is running at: 2019-02-09T02:33:16.280516Z
The constructor of GameControl
is running at: 2019-02-09T02:33:16.280796Z
Current GameControl id
: b74d9924-8f96-4321-9eca-a104642fc3f8
The reset
method is running at: 2019-02-09T02:33:16.281139Z
The constructor of GameControl
is running at: 2019-02-09T02:33:16.281213Z
Current GameControl id
: 3d8223c6-f93f-4708-832d-6cf60154befa