Java Reference
In-Depth Information
public class PayrollDemo
{
public static void main(String[] args)
{
// First two objects of Employee class are created using the
// parameterized constructor
Employee emp1 = new Employee("Jane", "131 Calm Street",
"263", 2);
Employee emp2 = new Employee("Jim", "42 Curve Road", "261",
6);
// Third employee object is created with non-parameterized
// constructor
Employee emp3 = new Employee();
// Fourth employee is created with partially parameterized
// constructor
Employee emp4 = new Employee("Mary");
// Display data using an accessor method
emp1.showData();
emp2.showData();
emp3.showData();
emp4.showData();
// Display total number of employees using a class method
Employee.showTotal();
}
}
The class Employee in the PayrollDemo.java program listed above con-
tains three constructors, declared as follows:
public Employee(String n, String a, String s, int x)
public Employee()
public Employee(String n)
The first constructor has a four-parameter signature. Of these parame-
ters three are strings and one is of type int. The second constructor is
parameterless. This constructor overrides the default constructor. The
third constructor receives a single parameter, of type string.
Programmers note:
The three constructors can coexist in the same class because they
have unique signatures.
The data elements for the class Employee are declared as follows:
// field variables
Search WWH ::




Custom Search