I saw this part of a code on the internet:
I found it looking for something I needed but it did not change the functionality of the rest of the code, I wanted to know what it was used for.
System.exit();
causes the Java VM to terminate completely.
JFrame.dispose();
causes the JFrame window to be destroyed and cleaned by the operating system. According to the documentation , this may make the Java virtual machine end if there is no other window available, but this really should be seen as a side effect instead of the norm.
The one you choose really depends on your situation. If you want to finish everything in the current Java virtual machine, you must use System.exit()
and everything is cleaned. If you only want to destroy the current window, with the side effect that will close the Java VM if this is the only window, then use JFrame.dispose()
.
With that instruction you close the current window and release the resources that window has been occupying.
this
It is a Java reserved word to reference the current object . When used together with dispose()
will then refer to the current window , so it is explained in the description of dispose()
.
For more details on this
, you can see this answer .
dispose
dispose()
is a method of the class Window
which, according to the documentation:
Releases all native screen resources used by this window, its subcomponents and all its children. That is, the resources for these components will be destroyed, any memory they consume will be returned to the operating system and marked as not visible.
The window and its subcomponents can be shown again rebuilding native resources with a subsequent call to
pack
orshow
(the latter was declared obsolete at be replaced bysetVisible
. of the recreated window and its subcomponents will be identical to the states of these objects in the point at which the Window was arranged (not counting the modifications additional among those actions).Note: When the last viewable window inside the Java virtual machine (VM), the VM can finish. See Topics for AWT sub-processes for more information.