So these things basically initialize variables of an object. Why do they need to do that exactly? I guess I don't really understand their meaning completely and having trouble comprehending it if anyone can help. I even looked up a Youtube video on it and the guys code just gave me errors everywhere!
I just made a new enemy, my enemy has vars in his constructor, hp and dmg, I just gave him 1000 health and 20 dmg.
Youll also learn about overloading. for example one constructor will take two int values, while you can overload and have another take 1 string and two ints, what this means is I can do the code I just did, OR i can
Enemy1 myenemy = new Enemy1("evil chicken", 1000, 20) , now I just gave my new object a name
Keep in mind my java is rusty, our Enemy1 object would look like this
Constructors are there to initialize not just the variable of your object, but also every variable inside the object as well.
For example, if you have some object X that has an int foo, then that's all well and good, except that until the object is initialized, those variables don't yet exist in memory, and are basically just placeholders. There's a lot of behind-the-scenes stuff that happens in the JVM, and handling this kind of stuff is one of its jobs. It does it so we the programmers don't have to.
Also, the constructor allows you to customize it with variable assignments, such as foo = 5. Sure, you can do this after you call the constructor in your main class, but we programmers are lazy and don't like writing code that we don't have to (hence methods).
So all-in-all, a constructor activates some function in the JVM that allocates the memory needed for that object and all its embedded variables, and its nice for convenience as well.
Rollback Post to RevisionRollBack
How to make spoiler boxes
To post a comment, please login or register a new account.
So these things basically initialize variables of an object. Why do they need to do that exactly? I guess I don't really understand their meaning completely and having trouble comprehending it if anyone can help. I even looked up a Youtube video on it and the guys code just gave me errors everywhere!
Enemy1 myenemy = new Enemy1(1000, 20)
I just made a new enemy, my enemy has vars in his constructor, hp and dmg, I just gave him 1000 health and 20 dmg.
Youll also learn about overloading. for example one constructor will take two int values, while you can overload and have another take 1 string and two ints, what this means is I can do the code I just did, OR i can
Enemy1 myenemy = new Enemy1("evil chicken", 1000, 20) , now I just gave my new object a name
Keep in mind my java is rusty, our Enemy1 object would look like this
thats what our object would look like for exmaple to make all that happen.
@StatusQ3: Go
Constructors are there to initialize not just the variable of your object, but also every variable inside the object as well.
For example, if you have some object X that has an int foo, then that's all well and good, except that until the object is initialized, those variables don't yet exist in memory, and are basically just placeholders. There's a lot of behind-the-scenes stuff that happens in the JVM, and handling this kind of stuff is one of its jobs. It does it so we the programmers don't have to.
Also, the constructor allows you to customize it with variable assignments, such as foo = 5. Sure, you can do this after you call the constructor in your main class, but we programmers are lazy and don't like writing code that we don't have to (hence methods).
So all-in-all, a constructor activates some function in the JVM that allocates the memory needed for that object and all its embedded variables, and its nice for convenience as well.