example of encapsulation in java

Encapsulation is a way of protecting the data in a class by hiding the implementation details from the outside world. This is done by making the variables private and providing public methods to access and modify those variables.

Here is an example of encapsulation in Java:

```java
public class Person {
private String name;
private int age;

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 class Main {
public static void main(String[] args) {
Person person = new Person();

person.setName("John");
person.setAge(30);

System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
}
}
```

In this example, the `Person` class has private variables `name` and `age`, and public getter and setter methods to access and modify these variables. This way, the `name` and `age` variables are encapsulated within the `Person` class and cannot be accessed directly from outside.