"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 = new MyFile();
file.initialize("c:\data\a.txt");
String text = file.readText();
MyFile file = new MyFile("c:\data\a.txt");
String text = file.readText();
MyFile file = new MyFile();
file.initialize("c:\data\", "a.txt");
String text = file.readText();
MyFile file = new MyFile("c:\data\", "a.txt");
String text = file.readText();
MyFile file = new MyFile();
file.initialize("c:\data\a.txt");
MyFile file2 = new MyFile();
file2.initialize( MyFile file, "a.txt");
String text = file2.readText();
MyFile file = new MyFile("c:\data\a.txt");
MyFile file2 = new MyFile(file, "a.txt");
String text = file2.readText();
"I just finished learning about the initialize method…"
"Look harder. With constructors, the code is more convenient and compact."
"So it is. Here's a question. I know how to write an initialize method inside a class, but how do I write a constructor?"
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?
what a short, helpful way to introduce constructors! Most textbooks take a long way of introducing constructors and only then explaining their functionality.
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.
GO TO FULL VERSION