Methods in Java

Methods in Java a written with a name in cameCase, preceeded by a return-type (or void for method that returns nothing), followed by a typed parameter list (which can be empty) and a body which consists of zero or more statements enclosed in brackets.

static void doSomething() {
  System.out.println("Do something...");
}

We will go into more details about the modifier static later. For now it’s just a way to compile our code.

Variables in Java are scoped to the method they where declared. See also Local Variable Scope#.

Parameter Passing

In Java, parameters are passed by value. In other words, they receives a copy of the original value, and what happens in the method does not affect the original object. See also Pass by Reference vs Pass by Value#.

int myVal = 5;

static void doubleVal(int val) {
  val *= 2;
  System.out.println(val); // 10
}

System.out.println(myVal); // 5