You may understand loops yet still put everything inside main, or copy a method without knowing where data enters and a result returns. A method declaration has parameters, a body and a return type; a method call supplies arguments and receives a result, and the example passes three scores through several methods.
What a method is and how to read its declaration
A method is a named block of behaviour declared inside a class and invoked when needed. Here, total adds scores, average divides by the subject count, and grade produces A, B or C. Methods are not constructors or free-standing functions.
Read this declaration:
static double average(int totalScore, int subjectCount)static is a modifier, double the result type, average the name, and the parenthesised items typed formal parameters. Braces contain the body. By contrast, the invocation average(254, 3) supplies arguments 254 and 3 and produces about 84.6667 before formatting.
You can learn Java through a structured language course or browse Coding & Skills courses

Build one complete Java program with three cooperating methods
Save this as ScoreMethods.java, then compile and run it.
public class ScoreMethods {
static int total(int javaScore, int dbmsScore, int osScore) {
return javaScore + dbmsScore + osScore;
}
static double average(int totalScore, int subjectCount) {
return (double) totalScore / subjectCount;
}
static String grade(double averageScore) {
if (averageScore >= 80) {
return "A";
} else if (averageScore >= 60) {
return "B";
}
return "C";
}
public static void main(String[] args) {
int totalScore = total(78, 86, 90);
double averageScore = average(totalScore, 3);
String finalGrade = grade(averageScore);
System.out.println("Total: " + totalScore);
System.out.printf("Average: %.1f%n", averageScore);
System.out.println("Grade: " + finalGrade);
}
}Trace three steps:
total(78, 86, 90)calculates78 + 86 + 90 = 254.average(254, 3)calculates254.0 / 3 = 84.666....grade(84.666...)takes the first branch because the value is at least80, then returnsA.
The output is:
Total: 254
Average: 84.7
Grade: Amain controls the sequence; each helper has one job. Without (double), two int operands undergo integer division before conversion. Formatting changes only the display, so the stored average keeps its precision.

Parameters, arguments, return values and void
In total(78, 86, 90), the three named score parameters exist only inside the method. The numbers are caller-supplied arguments. Java passes argument values into parameters, so changing a primitive parameter does not rewrite the caller's variable:
static void addFive(int score) { score += 5; }
int score = 40;
addFive(score);
System.out.println(score); // 40The local copy changes, so the output remains 40. The Java Language Specification defines this parameter and invocation behaviour. It is not pass by reference.
total(...) returns 254 for storage. A void method acts without producing a value:
static void printGrade(String value) {
System.out.println("Grade: " + value);
}return; can leave a void method early. A non-void method needs a compatible value on every reachable completion path.
Static methods versus instance methods
The static main calls these static helpers directly. Another class could use ScoreMethods.total(78, 86, 90). A static method has no current instance.
An instance method works through an object:
public class RectangleDemo {
static class Rectangle {
int width = 7;
int height = 4;
int area() {
return width * height;
}
}
public static void main(String[] args) {
Rectangle box = new Rectangle();
System.out.println(box.area());
}
}The output is 28. Call area() through box because it reads fields. Use an instance method for object-dependent behaviour and a static method when no instance is needed. Before adding static, ask whether fields are required.
Method overloading without ambiguity
Overloads share a name in one class but have different parameter lists:
static int area(int side) { return side * side; }
static int area(int length, int breadth) { return length * breadth; }area(6) returns 36; area(6, 4) returns 24. The compiler uses argument count and types. Given show(int value) and show(double value), show(7) selects int, while show(7.5) selects double.
Changing only the return type is illegal. static int pick(int n) and static double pick(int n) conflict because pick(5) cannot choose by result type. A method signature uses the name and parameter types, not the result type.
Common method errors and their exact fixes
Separate compiler errors from logic mistakes:
Error | Consequence | Exact fix |
|---|---|---|
Declare | Method declaration is illegal there | Move it to the class body |
Call | Wrong argument count | Supply the third |
Call |
| Pass or parse an |
Leave a reachable path without a result in an | Compilation fails | Return an |
Call |
| Create an object and call |
Two mistakes compile. return totalScore / subjectCount; uses integer division for two int operands, so cast one. Ignoring grade(84.666...) loses the returned "A", so store or use it.
Debug by reading the declaration, checking argument counts and types, inspecting return paths, deciding whether an object is needed, and testing the result. Making everything static is not a general fix.
How exams and interviews test Java methods
Common tasks include tracing nested calls, choosing a legal overload, distinguishing static from instance access, finding a missing return, and predicting whether a primitive argument changes.
static int increment(int n) { return n + 1; }
static int twice(int n) { return n * 2; }
System.out.println(twice(increment(4)));Work from the innermost call outward. increment(4) returns 5; twice(5) returns 10; the program prints 10. Write each parameter value beside its method. For the scores: 78, 86, 90 -> 254 -> 84.666... -> A.
You can learn how helper methods contribute to running time. For formal declaration, parameter, result, body, signature, static, instance and overloading rules, consult Oracle's Java SE 26 Language Specification
Practice checks, the short version and the next step
Try these before reading the expected values:
Write
maxOfThree(int a, int b, int c)and callmaxOfThree(12, 7, 19).Write
isEven(int n)and test both42and17.Overload
minutes:minutes(int hours, int extraMinutes)should convert hours and extra minutes, whileminutes(int value)should return an already converted value.
Expected values: 19; true for 42 and false for 17; 2 * 60 + 15 = 135; and 90 for minutes(90).
Declare methods in a class. Choose static only when no instance is needed. Type each parameter and pass compatible arguments. Use void for no result; otherwise return a compatible value on every path. Vary parameters when overloading.
Now change the scores to 55, 62 and 69. The total is 55 + 62 + 69 = 186; the average is 186.0 / 3 = 62.0; grade(62.0) takes the second branch and returns B.
Next, apply methods in data-structure and interview problems. The sorting algorithms comparison and dynamic programming show algorithms decomposed into focused methods. Change inputs, predict every result, then run the code to check your trace.




