A class is a blueprint that describes the features and actions of an object in Object Oriented Programming hence it is referred to as a cookie cutter. A class has fields that define the features, methods for actions, and constructors to instantiate the class. The concept is the same across all the programming languages but how to use them differs so today we will see how we can use class in Java.
List of Contents
The basic structure of a class in Java looks like this.
public class Person {
private String firstName;
private String lastName;
private int age;
public Person() {
}
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Modifier
A modifier comes the first of a code line and specifies the accessibility of a class and fields and methods in the class
Modifier | Scopes |
Public | Every class in the project can access |
Protected | Every class in the package and subclasses of the class even in the different packages can access |
default (When empty) | Every class in the same package can access |
Private | Only accessible within the class |
Encapsulation
It is the convention to set a class as public and the fields in the class as private, not allowing other classes to access them directly. This practice is called 'encapsulation' and the purpose of this is to prevent directly changing the values of the fields.
▶ Getters, Setters
So with encapsulation, we need a way to access fields to set values and retrieve them. The getter and setter are methods for the job.
Instantiation
Person person = new Person();
If we do not set values after instantiation primary types are set to default values as below.
Types | Default Values |
boolean | false |
byte | 0 |
short | |
int | |
long | |
char | |
double | 0.0 |
float |
Constructor
Used to instantiate a class. By default a no-argument constructor is available but if you add a constructor with arguments then the no-argument constructor needs to be added too to be available.
Static vs Instance
There are two ways to define a class field and method. One is the static and the other is the instance which can be differentiated by appending the Static keyword. The differences are as follows
Static
1. Class scope shares the class memory
2. No instantiation is needed. Conventionally, it is called with class name followed by dot notations
3. Static method cannot use This
4. Cannot be used in the instance method (To call static, the caller should be static too)
Instance
1. Needs instantiation to access
2. Memories are assigned to the instantiated object
So far we have seen the class in Java.
'Backend > Java' 카테고리의 다른 글
Lombok (0) | 2023.12.29 |
---|---|
Inheritance (0) | 2023.12.28 |
Switch (0) | 2023.12.26 |
Spring Boot Form (10) | 2023.10.28 |
Spring Beans - Dependency Injection (1) | 2023.10.07 |