0 day streak
java / beginner
Snippet

Private Access Modifier

The 'private' modifier restricts visibility to the defining class only, protecting the data from direct external modification.

snippet.java
java
1
2
3
4
5
6
class Account {
private double balance;
public void deposit(double amount) {
balance += amount;
}
}
Breakdown
1
private double balance;
Declares a private variable that cannot be accessed directly from outside the class.
2
public void deposit(...)
Provides a public method to safely modify the private balance.