Language syntax

Java Instance method

Invoked on an object receiver; can access its state subject to access control.

Formal Syntax & Structure

java syntaxjava
ReturnType methodName(Parameters) { /* body */ }

Working Example

Example Snippetjava
class Shelf {    private int count;    void addOne() { count++; }    int count() { return count; }}public class Main {    public static void main(String[] args) {        Shelf shelf = new Shelf();        shelf.addOne();        shelf.addOne();        System.out.println(shelf.count());    }}
Java Reference