"Now it's high time that I tell you about constructors. This is really simple concept. Programmers have invented a shorthand way to create and initialize objects."
Without a constructor
With a constructor
MyFile file =newMyFile();
file.initialize("c:\data\a.txt");String text = file.readText();
MyFile file =newMyFile("c:\data\a.txt");String text = file.readText();
MyFile file =newMyFile();
file.initialize("c:\data\", "a.txt");String text = file.readText();
MyFile file =newMyFile("c:\data\", "a.txt");String text = file.readText();
I think codeGym had us do the initialize methods first to just used to the idea how or what purpose Constructors are used for, mainly, to initialize Instance Variables. By now, we're used to the idea of initializing variables and we just need to apply it to Constructors and use it going forward.
I really don't understand the difference between constructor and inizialize method. When I have to use one instead of other? or contructor is just a compact inizialize method?
The main difference is initialize method is used AFTER you already created the object. Constructor is used at the time of creating the object.
So it depends if you need your instance variables at the time of creating the object, or after the object has already been created. Most programs I've seen in Java mainly use the constructor. I barely ever see initialize being used.
Hope this helps.
Some Frameworks will use initialization extensively. Anything where you have ...
ServiceClass someServiceClass = Class.forName("com.yourcompany.projectname.package.classname).newInstance();
someServiceClass.initialize() <-- this initialize method can then call another set of Class.forName methods, or do some other initialization using system properties or other means to 'initialize' the classes using a given environment ... so if the above was a database connection, it could get initialized with database name, url, uname, password, etc. This keeps the config out of the code, lets the framework instantiate the objects via default means (caller does not need to know about what database is used on the back end, just need it to get initialized to 'some' database), and allows the framework to keep track of object state (so if an error occurred during initialization for example, the someServiceClass could have a .isSafeToUse method that would be set to false initially, then only set to true if all initialization steps were successful, otherwise the caller would know not to use it without needing to know how to use it.
We can also use static code blocks for this, eliminating the need for the caller to interact with the framework code.
what a short, helpful way to introduce constructors! Most textbooks take a long way of introducing constructors and only then explaining their functionality.
In reality, you will likely always use constructors instead of the initialize method. This forces the user of a class to initialize the instance with the correct information, as opposed to hoping they remember to call the initialize method. In my years as a professional software developer, I've honestly never seen the initialize method used. Take a look through some Java projects on GitHub and I'm sure you'll be able to confirm this.
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.