Definition (#24775)

This commit is contained in:
Alyssa Dang
2018-12-13 21:39:28 -06:00
committed by Manish Giri
parent 767cd0e31a
commit b0fe051522

View File

@ -1,10 +1,9 @@
--- ---
title: Constructors title: Constructors
--- ---
Constructors are functions or methods in a program that creates an object of a class and can also initialize attributes for the object. The object is an instance of the class and can perform the methods/functions defined within the class.
If an object copies from a class, the what's the point? I should be able to store data in it right? This includes **getter** (e.g., getName()) / **setter** (e.g., setName()) methods. Basically, every Java Class has a constructor which is the method called first when any object of the class is initialized. Think of it as the creation of a new attribute, not unlike the declaration of a new data type.
That's when we use either **getter** (e.g., getName()) / **setter** (e.g., setName()) methods, or in this case constructors, to initialize a class. Basically, every Java Class has a constructor which is the method called first when any object of the class is initialized. Think of it as a bit of starter code.
When you write a class without any constructor, the Java compiler creates a default constructor : When you write a class without any constructor, the Java compiler creates a default constructor :