-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetnSet.java
More file actions
40 lines (33 loc) · 934 Bytes
/
GetnSet.java
File metadata and controls
40 lines (33 loc) · 934 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class GetnSet {
public static void main(String[] args) {
// Create a new GetnSet object
GetnSet person = new GetnSet();
// Set the values using setters
person.setName("John Doe");
person.setAge(30);
// Get the values using getters
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
}
// Private fields
private String name;
private int age;
// Getter for 'name'
public String getName() {
return name;
}
// Setter for 'name'
public void setName(String name) {
this.name = name;
}
// Getter for 'age'
public int getAge() {
return age;
}
// Setter for 'age'
public void setAge(int age) {
if (age > 0) { // Adding a simple validation
this.age = age;
}
}
}