Java OOP · Module 0
Methods and arrays
Naming behaviour, and holding many values
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
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
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 hands a value back and ends the method immediately. 2.0 keeps the division decimal.Words that will appear in every later module
int a).average(80, 90)).void means nothing is returned.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.
Add a third call with your own text, then run.
Arrays
Arrays hold many values of one type
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
static int twice(int n) { System.out.println(n * 2); }grades.length()for (int i = 0; i <= grades.length; i++)Lesson playground
A method that averages an array. Add a grade to the array and re-run.
Check understanding
Quiz
Question 1 of 4
What does this print?
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.