|
The general pattern for declaring a new object looks like this:
The new Student-type object "Bill" must construct itself. Therefore in the Student object, there will be a method that creates this new object. Constructors have the same name as their class. Therefore in the class "Student", there would be a constructor also named "Student()". As illustrated below, a class may have numerous constructors, all named "Student()". Note the parentheses. They are useful for sending information into the new object. Suppose that I create a class called registrar, which will create enrollees:
public class registrar {
public static void main(String[] args) {
enrollee a = new enrollee();
} // main
} // registrar
and furthermore, create a object called enrollee. In this default version, the new enrollee gets the default name. Below, the default constructor has been hilighted.
public class enrollee {
String Name = new String("Unknown student");
enrollee() {
showData();
} // default constructor
void showData() {
System.out.println(Name);
} // showData()
} // class enrollee
Data can also be sent into the constructor of an object. Elaborate registrar by adding some new lines:
public class registrar {
public static void main(String[] args) {
System.out.println("Here's the default constructor:");
enrollee a = new enrollee();
System.out.println("Here's a one-parameter constructor:");
enrollee b = new enrollee("Bilbo Bobbins");
} // main
} // registrar
and elaborate enrollee by adding a one-parameter constructor (it has been hilighted):
public class enrollee {
String Name = new String("Unknown student");
enrollee() {
showData();
} // default constructor
enrollee(String inName) {
Name = inName;
showData();
} // one-parameter constructor
void showData() {
System.out.println(Name);
} // showData()
} // class enrollee
Constructors can have as many parameters as necessary. Elaborate registrar once again:
public class registrar {
public static void main(String[] args) {
System.out.println("Here's the default constructor:");
enrollee a = new enrollee();
System.out.println("Here's a one-parameter constructor:");
enrollee b = new enrollee("Bilbo Bobbins");
System.out.println("A three-parameter constructor:");
enrollee c = new enrollee("Candy Cotton", 23, "hockey");
} // main
} // registrar
and elaborate enrollee with a three-parameter constructor. Given that we are sending in new data, there is a new method for printing it all out.
public class enrollee {
String Name = new String("Unknown student");
int Age = 0;
String Sport = new String("No sport");
enrollee() {
showData();
} // default constructor
enrollee(String inName) {
Name = inName;
showData();
} // one-parameter constructor
enrollee(String inName, int inAge, String inSport) {
Name = inName;
Age = inAge;
Sport = inSport;
showAllData();
} // three-parameter constructor
void showAllData() {
System.out.println(Name + " is " + Age + " years old and loves to play " + Sport);
} // showAllData()
void showData() {
System.out.println(Name);
} // showData()
} // class enrollee
|