Concept

What is a constructor and how is it written?

ComputerScienceOne / Constructors

"A constructor is a special method that specifies how an object is constructed. With built-in primitive variables such as an int, the Java language (compiler and JVM) “know” how to interpret and assign a value to such a variable. However, with user-defined objects such as our Student class, we need to specify how the object is created. A constructor method has special syntax. Though it may still one of the visibility keywords, it has no return type and its name is the same as the class. A constructor may take any number of parameters. For example, the following constructor allows someone to construct a Student instance and specify all four member variables.\n\n1 public Student(String firstName, String lastName,\n\n2 int id, double gpa) {\n\n3 this.firstName = firstName;\n\n4 this.lastName = lastName;\n\n5 this.id = id;\n\n6 this.gpa = gpa;\n\n7 }"

Related Ideas

What is a constructor and how is it written? | ComputerScienceOne | Bifalgorithm | Bifalgorithm