0% found this document useful (0 votes)
3 views3 pages

Java Encapsulation Example of Getter Setter

Uploaded by

paludit337
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java Encapsulation Example of Getter Setter

Uploaded by

paludit337
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Encapsulation: Implementing Person

Class with Getter and Setter Methods


Sample Solution:

Java Code:
// Person.java

// Person Class

class Person {

private String name;

private int age;

private String country;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public int getAge() {

return age;

public void setAge(int age) {


this.age = age;

public String getCountry() {

return country;

public void setCountry(String country) {

this.country = country;

Copy
// Main.java

// Main Class

public class Main {

public static void main(String[] args) {

// Create an instance of Person

Person person = new Person();

// Set values using setter methods

person.setName("Arthfael Viktorija");

person.setAge(25);

person.setCountry("USA");
// Get values using getter methods

String name = person.getName();

int age = person.getAge();

String country = person.getCountry();

// Print the values

System.out.println("Name: " + name);

System.out.println("Age: " + age);

System.out.println("Country: " + country);

You might also like