Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 0

Methods and arrays

Naming behaviour, and holding many values

14m35 XP
→→→

What you'll be able to do

  • Write a method with parameters and a return value
  • Explain what `void` means and when to use it
  • Create an array, read it by index, and walk it with for-each
  • See why methods are the bridge to object behaviour in Module 2
1

Methods

A method is a named piece of behaviour

Objects are data plus behaviour. Methods are how behaviour is written — so this is the last stop before OOP.

Anatomy of a method

Main.java
1static double average(int a, int b) {
2 return (a + b) / 2.0;
3}
static double average(int a, int b) {
double is the return type — what the caller gets back. average is the name. (int a, int b) are parameters: local variables filled in by the caller.
return (a + b) / 2.0;
return hands a value back and ends the method immediately. 2.0 keeps the division decimal.

Words that will appear in every later module

Parameter
The variable declared in the method header (int a).
Argument
The actual value you pass when calling (average(80, 90)).
Return type
The type of the value handed back; void means nothing is returned.
Signature
Name + parameter list. Java uses it to tell methods apart — essential for overloading in Module 9.

void vs a return value

Use void when the method *does* something (prints, updates a field). Use a return type when the method *answers* something (calculates, checks). deposit(...) is void; getBalance() returns a double.

Try it: call a method twice

Add a third call with your own text, then run.

Output
Press Run to execute this example.
2

Arrays

Arrays hold many values of one type

Create, read, walk
Main.java
1int[] grades = new int[] { 80, 90, 70 };
2System.out.println(grades[0]);
3System.out.println(grades.length);
4for (int grade : grades) {
5 System.out.println(grade);
6}
Indexes start at 0, so the last valid index is `length - 1`. `length` is a field, not a method — no parentheses.

An array has a fixed size: new int[3] always holds exactly three values, pre-filled with the default value of the type (0 for int, false for boolean, null for objects). In Module 13 you will store Account[] inside a Bank object — same idea, only the element type changes.

ArrayIndexOutOfBoundsException

Reading grades[3] from a 3-element array crashes at runtime, not at compile time. Loop with i < grades.length (never <=) and you will never see it.

Common mistakes

Wrongstatic int twice(int n) { System.out.println(n * 2); }
WhyThe return type promises an int but the method only prints and never returns.
FixEither `return n * 2;` or change the return type to `void`.
Wronggrades.length()
WhyArray length is a field; only String has a `length()` method.
Fixgrades.length — no parentheses.
Wrongfor (int i = 0; i <= grades.length; i++)
WhyOn the last round `i` equals `length`, which is one past the end.
FixUse `i < grades.length`.

Lesson playground

Free practice

A method that averages an array. Add a grade to the array and re-run.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 4

What does this print?

Main.java
1public class Main {
2 static int doubleIt(int value) {
3 return value * 2;
4 }
5
6 public static void main(String[] args) {
7 int[] numbers = new int[] { 3, 5, 8 };
8 int sum = 0;
9 for (int n : numbers) {
10 sum = sum + doubleIt(n);
11 }
12 System.out.println(sum);
13 System.out.println(numbers.length);
14 }
15}

Summary

  • A method = return type + name + parameters + body; `void` means it returns nothing.
  • Parameters are the placeholders, arguments are the real values you pass.
  • Arrays are fixed-size, zero-indexed, and expose `length` as a field.
  • You now have every prerequisite: the next module explains why all of this is not enough on its own.