What should be there in a Java Class? Understand the Class Components

Опубликовано: 19 Октябрь 2024
на канале: Learn with Professor
127
like

What should be there in a Java Class? Understand the Class Components
#tamilkaruvoolam #javaclass #crudeoil

Java Class Components
Class Template
Properties
Constructors
Getters and Setters
toString()
CRUD: Create / Retrieve / Update / Delete
For Create - Use Parameterized Constructor
For Update - Use Setter Methods
For Retrieve - Use Getter / toString Method
For Delete - Set object as NULL

Since, this tutorial focuses on basics, hashCode() method has not been used.

Source Code
/**
Demo Class for Employee

@author Anthoniraj Amalanathan
@since 24-Aug-2022
*/

class Employee {
private String name;
private Double salary;

public Employee() {
}
public Employee(String name, Double salary) {
this.name = name;
this.salary = salary;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [name=" + name + ", salary=" + salary + "]";
}
}

/**
Employee Test Class

@author Anthoniraj Amalanathan
@since 24-Aug-2022
*/

public class EmployeeTest {
public static void main(String[] args) {
Employee employee = new Employee("Kumar", 123456.50);
System.out.println(employee.toString());
System.out.println(employee.getSalary());
employee.setSalary(8576857.50);
System.out.println(employee.toString());

}
}