-
Notifications
You must be signed in to change notification settings - Fork 137
Java attributes and methods
Java attributes and methods are essential parts of HMIS development.
Attributes store information such as:
- Patient name
- Patient code
- Bill total
- Created date
- Department
- Investigation
- Report status
- Retired status
Methods are used to:
- Read attribute values
- Change attribute values
- Validate data
- Calculate totals
- Process reports
- Search database records
- Clear filters
- Format information for display
A simple HMIS-style example is:
private String patientCode;
private double total;
private boolean retired;
public String getPatientCode() { return patientCode; }
public void setPatientCode(String patientCode) { this.patientCode = patientCode; }
public double calculateNetTotal(double discount) { return total - discount; }
In this example:
- patientCode, total, and retired are attributes.
- getPatientCode(), setPatientCode(), and calculateNetTotal() are methods.
An attribute stores data inside a Java component. Attributes are also called:
- Fields
- Member variables
- Instance variables
- Properties, especially when accessed through getters and setters
General syntax : accessModifier dataType attributeName;
private String patientName;
- private is the access modifier.
- String is the data type.
- patientName is the attribute name.
Exmples
- private Long id;
- private String patientCode;
- private Date createdAt;
- private Department department;
- private Investigation investigation;
- private boolean retired;
- private List results;
An attribute can be declared without an initial value.
private String patientName;
It can also be declared with an initial value.
private boolean retired = false;
A collection should often be initialized immediately.
private List results = new ArrayList<>();
This is safer than:
private List results;
because using an uninitialized list may cause a NullPointerException.
For example :
results.clear();
will fail when results is null.
Attributes may use primitive types or reference types.
Primitive attributes
private int patientCount;
private long recordCount;
private double total;
private boolean retired;
private char categoryCode;
- Primitive types cannot contain null.
Reference attributes
private String patientName;
private Date createdAt;
private Department department;
private Investigation investigation;
private List bills;
- Reference attributes can contain null.
Primitive int long double boolean char