Language syntax
Java Public member access
A public member may be accessed when its declaring type is accessible. Public visibility alone does not guarantee a meaningful API contract.
Formal Syntax & Structure
java syntaxjava
public int quantity() { return 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()); }}