PCPF Solutions
PCPF Solutions
Example:
class Employee {
// Instance variables
private String name;
private int id;
Output:
Name: John Doe
ID: 101
2. Invoking Constructors
Example:
class Employee {
private String name;
private int id;
// Constructor 1
public Employee(String name) {
this(name, 0); // Calls Constructor 2
}
// Constructor 2
public Employee(String name, int id) {
this.name = name;
this.id = id;
}
Example:
public Employee setName(String name) {
this.name = name;
return this; // Allows method chaining
}
Example:
this.greet(this); // Passes the current object
Summary