/**
 * Copyright (C) 2024 by Martin Robillard. 
 * See https://codesample.info/about.html
 */
package essentials;

/**
 * Represents an employee in a company.
 */
class Employee
{
String name; String title = "Developer"; // Default value Department department;
} /** * Represents a department in a company. */ class Department { String name;
Department(String name) { this.name = name;
}
String getName() { return "Department of " + this.name;
} } /** * This class represents "client code" and it purpose is only to hold the main * method. */ class ClassesAndObjects { public static void main(String[] args) { // Create a new Employee object with the default constructor Employee employee = new Employee(); /* * Accessing fields directly is not a recommended practice. We do it * here only to illustrate the runtime structure of an object. */ employee.name = "Dilbert"; // Create a new Department object with an explicit constructor. Department department = new Department("Great Features");
employee.department = department;
System.out.println(employee.name); System.out.println(employee.title); System.out.println(employee.department.name); System.out.println(employee.department.getName()); } }