I am on my way of learning JAVA, and the more I learn, the more I realise how much I don't know. I am having trouble understanding object initialisation in details. (I didn't paste the whole code for the simplicity's sake). Thank you in advance.
I have a GUI class which creates the GUI, and I have a driver class called Calculator with the Main() method in it. My program works well, I just want to understand object initialisation in details, because I am a bit confused.
public class GUI {
GUI() {
}
public static void Init() {
// Code for creating GUI and elements
}
}
public class Calculator {
public static void main(String args[]) {
GUI main = new GUI(); // Option 1
new GUI; // Option 2
GUI.Init(); //Option 3
}
}
So my question is if I have a class with a no-arg constructor or constructor with data, what is the best approach to run the program? What is the best practice? Which approach should I use?
My understanding is:
GUI main = new GUI(); // This will initiate the object, but will not execute
new GUI; // This will initiate and execute my program and run
GUI.Init(); // This will run the method of GUI, therefore can be used to run the program if built that way.