Language syntax
Java package access
No access modifier on this ordinary class member allows use within the declaring package. Multi-file example: save each labeled section separately. Compile the listed files with javac --release 21 -d out and run java -cp out demo.app.Main.
Formal Syntax & Structure
java syntaxjava
int capacity() { return 100; }Working Example
Example Snippetjava
// File: src/demo/stock/Stock.javapackage demo.stock;public class Stock { private int quantity; public Stock(int quantity) { if (quantity < 0) throw new IllegalArgumentException("Negative stock"); this.quantity = quantity; } public int quantity() { return quantity; } int capacity() { return 100; }}
// File: src/demo/stock/StockReport.javapackage demo.stock;public class StockReport { public static String report(Stock stock) { return stock.quantity() + "/" + stock.capacity(); }}
// File: src/demo/app/Main.javapackage demo.app;import demo.stock.Stock;import demo.stock.StockReport;public class Main { public static void main(String[] args) { Stock item = new Stock(4); System.out.println(StockReport.report(item)); }}