Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Use Java class from another class

public class Calculator {
    public static Integer add(Integer a, Integer b) {
        if (a == 10) {
            return 10;
        }
        return a+b;
    }

}

public class UseCalc {
    public static void main(String[] args) {
        Number a = Calculator.add(2, 3);
        System.out.println(a);
        Number b = Calculator.add(10, 3);
        System.out.println(b);
    }
}

cd examples/mymath
rm -f *.class; javac Calculator.java UseCalc.java ; java UseCalc