Constructor or init() method?
in
Programming Questions
•
10 months ago
Here is a template class in Processing:
- class MyClass() {
- // Declare, define properties
- float property1;
- float property2;
- float property3;
- // Pass properties into the constructor
- MyClass(float temp1, float temp2, float temp3) {
- property1 = temp1;
- property2 = temp2;
- property3 = temp3;
- // Define methods
- void init() {
- }
- void update() {
- }
- void render() {
- }
- }
Now, here is another way I found someone writing the same class:
- class MyClass() {
- float property1;
- float property2;
- float property3;
- void MyClass() {
- }
- void init() {
- }
- void update() {
- }
- void render() {
- }
- }
Where is the constructor of the latter class? Is it MyClass() method or init() method? Since we can choose to instantiate an object in any method, not just in the constructor, can I assume that a constructor is in fact just another method in a class exclusively used to create starting objects? And if so, why do I need MyClass() method when I can always use init() method (or any methods for that matter) as a constructor?
Thanks in advance.
1