Language syntax
Java Private member access
A private member is inaccessible to unrelated top-level classes through normal Java access. Enclosing and nested classes have special access rules. Private visibility does not validate values.
Formal Syntax & Structure
java syntaxjava
private int quantity;Working Example
Example Snippetjava
class Stock { private int quantity; Stock(int quantity) { if (quantity < 0) throw new IllegalArgumentException("negative stock"); this.quantity = quantity; } public int quantity() { return quantity; } public boolean reserve(int units) { if (units <= 0 || units > quantity) return false; quantity -= units; return true; }}public class Main { public static void main(String[] args) { Stock stock = new Stock(5); System.out.println(stock.reserve(3)); System.out.println(stock.quantity()); System.out.println(stock.reserve(4)); System.out.println(stock.quantity()); }}