Model G20 2027 at FLAME University, registrations now open

User - defined Methods | ICSE Class 10 Computer Applications Notes

29 min read

On this page

This note covers the need for user-defined methods, method syntax and forms, method calls, actual and formal parameters, value passing, returned values, object creation, static and non-static methods, pure and impure methods, and method overloading.

What are user-defined methods and why are they useful?

A method is a named block of instructions declared within a Java class to perform a task. A class defines the data and behaviour of its objects. A user-defined method is written by the programmer to meet a particular requirement.

A program is a set of instructions for a computer. A statement is an instruction within that program. Defining a method makes its instructions available; calling or invoking it causes those instructions to run. The definition and the call therefore serve different purposes.

Property: Methods support modularity and reuse

Modular programming divides a program into smaller parts with specific responsibilities. Different method names can identify different tasks. A method may call another method, allowing a larger task to be assembled from smaller operations whose purposes are easier to follow.

  • Readability: named methods organise instructions and make longer programs easier to understand.
  • Reusability: the same method can be called wherever its operation is needed.
  • Reduced repetition: a repeated operation can be written once instead of copied into several places.
  • Debugging: finding and correcting errors becomes easier when the relevant operation is separated into a method.
  • Division of work: members of a programming team can work on separate tasks.

A useful method has a clear purpose. Decide what information it needs, what it does, and whether it should send a result back. These decisions help distinguish the work performed inside the method from the work performed by the method that calls it.

Data members are variables declared in a class outside its methods. A variable is a named storage location for a value. Member methods operate within the class structure. Together, data members and methods describe an object's state, meaning stored data, and behaviour, meaning operations.

Definition: A method definition describes an operation; a method call requests that operation. Reusing the definition does not require rewriting its complete body at every call.

How is a Java method declared and defined?

Syntax means the rules for writing valid language statements. A method's header gives its name, result type and parameters, together with any modifiers. Its body contains the instructions to execute. Parameters are named variables through which a method receives information from a call.

Syntax: modifiers returnType methodName(parameterList) { statements }

Here, modifiers, returnType, methodName, parameterList and statements are descriptive placeholders. Modifiers specify properties such as accessibility or class membership. The return type identifies the kind of value sent back; void specifies that no value is returned.

Parentheses, ( ), enclose parameters in a definition and arguments in a call. Empty parentheses mean there are none. Braces, { }, enclose a class or method body. A semicolon, ;, ends a simple statement. Java distinguishes uppercase from lowercase letters.

What does the program wrapper mean?

In the programs below, Functions is the class name. The keyword class introduces it. The modifier static associates a method with the class, while public permits access from other classes. The standard starting method used here is main.

String is Java's type for text. In main, args names the command-line argument parameter; [] means an array, an indexed collection of elements. No command-line input is used here. A keyword is a reserved language word; an identifier names a program element.

System.out.println displays a value and moves to a new output line. System is a library class; out is its standard output stream; println is the printing method. The dot, ., selects a member. Double quotation marks enclose a string literal, a text value written directly.

Worked example 1. Define and call a method that displays a message.

class Functions {
static void helloPython() {
System.out.println("I love Programming");
}
public static void main(String[] args) {
helloPython();
}
}

Answer: The output is:

I love Programming

  1. Line 1 opens the class; line 2 opens the static method helloPython, which needs no parameters and returns no value.
  2. Line 3 displays the message; line 4 closes the method body.
  3. Line 5 opens main. Line 6 calls helloPython, transferring execution to its body.
  4. Line 7 closes main; line 8 closes the class. The name helloPython is simply an identifier in this Java program.

For these concrete methods, the header is followed by a body. Do not insert a semicolon between the closing parameter parenthesis and the opening body brace. The semicolon belongs after the calling statement, as shown inside main.

What are the different forms of methods?

Two independent questions classify a method's form: does it receive parameters, and does it return a value? Input through parameters and output through a returned value describe different directions of information flow. A method may use either, both, or neither.

ParametersReturn valueMeaning
AbsentAbsentEmpty parameter list and void return type.
AbsentPresentEmpty parameter list and a specified result type.
PresentAbsentReceives argument values but returns no value.
PresentPresentReceives argument values and sends a result back.

A method without parameters can still use values stored in its body or in accessible data members. Similarly, a void method can display text or change stored data. Neither an empty parameter list nor the word void means that the method performs no work.

How can a method calculate without parameters?

The type int stores integers, meaning whole-number values within its supported range. The assignment operator, =, stores the value on its right in the variable on its left. With integer operands, + adds their values. An operand is a value used by an operator.

In the following program, fnum and snum store the first and second numbers, and sum stores their total. These are local variables, declared inside the method. Their data is supplied by initialisation, meaning assigning starting values when the variables are declared.

Worked example 2. Add the given integers 5 and 6 inside a method with no parameters.

class Functions {
static void addnum() {
int fnum = 5;
int snum = 6;
int sum = fnum + snum;
System.out.println(sum);
}
public static void main(String[] args) {
addnum();
}
}

Answer: The output is:

11

  1. Line 1 opens the class; line 2 defines addnum with no parameters and no returned value.
  2. Line 3 initialises fnum to 5; line 4 initialises snum to 6.
  3. Line 5 adds the values and stores 11 in sum. Line 6 displays that total.
  4. Line 7 closes addnum; line 8 opens main; line 9 calls addnum.
  5. Lines 10 and 11 close main and the class. Displaying the total does not return it to main.

To choose a form, begin with the task's requirements rather than the method's name. If a caller needs to use a calculated result in another expression, returning that value is useful. If the operation is simply to display information, a void method can perform that task.

How does a method call change the flow of execution?

Flow of execution is the order in which statements run. The caller is the method making a call; the called method receives it. Execution enters the called method, completes its work, and continues in the caller after the call has finished.

Arguments, also called actual parameters, supply values at the call. A comma separates successive arguments or parameter declarations. In the following example, first and last receive the first and last names. fullname is both the method name and, inside it, a local text variable.

For strings, concatenation means joining text. The + operator performs string concatenation when a string operand is involved. The literal containing a space places a gap between the two names; the literal "Hello " also includes a trailing space.

Worked example 3. Pass the names Gyan and Vardhan to a method and display the greeting.

class Functions {
static void fullname(String first, String last) {
String fullname = first + " " + last;
System.out.println("Hello " + fullname);
}
public static void main(String[] args) {
fullname("Gyan", "Vardhan");
}
}

Answer: The output is:

Hello Gyan Vardhan

  1. Line 1 opens the class. Line 2 declares fullname with two String parameters and a void return type.
  2. Line 3 joins first, a space and last, storing the resulting text in the local variable fullname.
  3. Line 4 displays the greeting. Line 5 closes the called method.
  4. Line 6 opens main; line 7 supplies the two name literals in order.
  5. Lines 8 and 9 close main and the class. The call completes after the greeting has been displayed.

What should a trace record?

A trace follows execution and records changing values and output. At each call, identify the selected method, match the supplied values to its parameters, and execute its statements. Then return to the caller. Do not treat the position of a definition as a call.

The order of method definitions within a Java class does not determine their calling order. In these programs, main starts the work and its calls determine which methods run. A method can be declared later in the class than another method that calls it.

How do actual and formal parameters correspond?

Property: Arguments correspond to parameters in order

A formal parameter is a variable declared in a method's parameter list. An actual parameter is an argument supplied in the calling expression. Java evaluates the argument and supplies its value to the corresponding formal parameter. The correspondence follows position, not variable names.

Arguments must be compatible with the method's declared parameter types. A data type specifies the kind of value a variable can hold. The call must also supply an appropriate number of arguments. For ordinary positional parameters, changing the order can change the operation's meaning.

How is an integer passed into a repeated calculation?

In the next program, num is the caller's variable and n is the formal parameter. Natural numbers here mean the positive integers starting at 1. i is the loop counter, a variable that tracks successive repetitions; sum accumulates the total.

A for loop repeats a statement using initialisation, a continuation condition and an update. The operator <= means less than or equal to; ++ increases a variable by one. The loop continues while its condition is true and stops when it becomes false.

Worked example 4. Pass 5 to sumSquares and add the first 5 natural numbers. Despite its name, this method adds the numbers themselves.

class Functions {
static void sumSquares(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum = sum + i;
}
System.out.println(sum);
}
public static void main(String[] args) {
int num = 5;
sumSquares(num);
}
}

Answer: The output is:

15

  1. Line 1 opens the class; line 2 declares the formal integer parameter n.
  2. Line 3 starts sum at zero. Line 4 starts i at 1 and repeats while i is at most n, increasing i after each repetition.
  3. Line 5 adds the current i to sum; line 6 closes the repeated block.
  4. Line 7 displays the accumulated total; line 8 closes sumSquares.
  5. Line 9 opens main; line 10 gives num the value 5; line 11 passes that value into n.
  6. Lines 12 and 13 close main and the class. The addition performed is 1 + 2 + 3 + 4 + 5.

Scope is the part of a program where a variable can be accessed by its name. The parameter n is used inside sumSquares; num is local to main. Giving them different names makes their distinct roles visible, but matching names would not make them the same variable.

What does passing by value mean in Java?

With pass by value, the called method receives a copy of the argument's value. For a primitive type, such as int, that value is the number itself. Assigning a different number to the parameter does not assign a new number to the caller's variable.

In the next program, number is the variable in main, while num is the parameter inside incrValue. The method adds 5 to its own parameter. The unchanged caller variable is displayed after the method finishes, so both values can be distinguished.

Worked example 5. Call incrValue with number initially equal to 8, then display number after the call.

class Functions {
static void incrValue(int num) {
num = num + 5;
System.out.println(num);
}
public static void main(String[] args) {
int number = 8;
incrValue(number);
System.out.println(number);
}
}

Answer: The output, on separate lines, is:

13
8

  1. Line 1 opens the class; line 2 declares incrValue with its integer parameter num.
  2. Line 3 replaces num's copied value with that value plus 5. Line 4 displays 13.
  3. Line 5 closes incrValue. Line 6 opens main, and line 7 initialises number to 8.
  4. Line 8 passes a copy of 8 to the method. Line 9 displays the caller's number, which remains 8.
  5. Lines 10 and 11 close main and the class. No return value is assigned to number.

How should call by reference be distinguished?

Call by reference, as a general programming mechanism, gives a parameter access to the caller's variable itself. For example, swapping variables through true reference parameters can exchange the values held in the caller's original variables. This is a conceptual example, not Java parameter behaviour.

An object is an instance of a class, and a reference is a value that identifies an object. Java also copies reference values when passing object arguments. The caller and parameter can consequently refer to the same object without becoming the same variable.

Note: Java passes arguments by value. A method can use a copied object reference to change accessible data in the shared object. Reassigning the parameter to a different object does not reassign the caller's reference variable.

Keep the distinction between changing an object and reassigning a variable. Shared access to an object's data explains why some changes remain visible after a call. It does not establish that Java uses true call by reference.

How does a method return a value to its caller?

Property: Return transfers control to the caller

The keyword return ends execution of the current method and transfers control to its caller. In a value-returning method, return also supplies a result. The returned expression must be compatible with the declared return type. An expression combines values and operations to produce a value.

Printing and returning are separate operations. Printing sends text to the output stream. Returning supplies a value for the calling expression. A caller may store that value in a variable, display it, or use it in another calculation.

How can the caller use a calculated result?

A base is the number repeatedly multiplied in a power; a non-negative integer exponent tells how many such factors are used. In calcpow, number receives the base, power receives the exponent, and result accumulates the product. The operator * means multiplication.

In main, base and expo hold the supplied values, while answer stores the returned result. The method below uses repeated multiplication and is intended for non-negative integer exponents whose results fit in int.

Worked example 6. Calculate 5 raised to the power 4 and return the result.

class Functions {
static int calcpow(int number, int power) {
int result = 1;
for (int i = 1; i <= power; i++) {
result = result * number;
}
return result;
}
public static void main(String[] args) {
int base = 5;
int expo = 4;
int answer = calcpow(base, expo);
System.out.println(answer);
}
}

Answer: The output is:

625

  1. Line 1 opens the class; line 2 defines calcpow with an int return type and two integer parameters.
  2. Line 3 initialises the product to 1. Line 4 sets up power repetitions.
  3. Line 5 multiplies the current result by number; line 6 closes the loop.
  4. Line 7 returns the product; line 8 closes calcpow.
  5. Line 9 opens main. Lines 10 and 11 store the base 5 and exponent 4.
  6. Line 12 calls calcpow and stores its returned value in answer. Line 13 displays 625.
  7. Lines 14 and 15 close main and the class.

Rule: A value-returning method must return an appropriate value on every execution path that completes normally. A void method cannot return a value, although a bare return statement can end it early.

In the worked program, calcpow itself performs no printing. Its returned integer remains available to main through answer. Following this separation prevents the common mistake of declaring a method void while expecting its call to supply a value for an assignment.

How do static and non-static methods differ?

A static method belongs to the class and can be called using the class name. A non-static method, also called an instance method, is invoked on an object. An object supplies the instance context in which that method operates.

Inside a static method, an instance method cannot be called without an appropriate object reference. A static method also needs an object reference to access instance data. Earlier examples used static methods that main could call directly within the same class.

How does object creation enable a call?

The keyword new creates an object. In new Functions(), Functions() invokes a constructor, the special class member used when initialising an object. The variable functions below stores the resulting reference. The expression functions.calcFact(5) selects the object's method and supplies its argument.

A positive integer's factorial is the product of the positive integers from that number down to 1. In calcFact, num is the supplied integer and fact stores the product. The operator > means greater than; -- reduces a variable by one.

Worked example 7. Create an object and use its instance method to display the factorial of 5.

class Functions {
void calcFact(int num) {
int fact = 1;
for (int i = num; i > 0; i--) {
fact = fact * i;
}
System.out.println(fact);
}
public static void main(String[] args) {
Functions functions = new Functions();
functions.calcFact(5);
}
}

Answer: The output is:

120

  1. Line 1 opens the class. Line 2 declares calcFact without static, making it an instance method.
  2. Line 3 starts fact at 1; line 4 counts i down from num while i remains positive.
  3. Line 5 multiplies fact by the current i; line 6 closes the loop.
  4. Line 7 displays the product; line 8 closes calcFact.
  5. Line 9 opens main. Line 10 creates a Functions object and stores its reference in functions.
  6. Line 11 invokes calcFact on that object with argument 5. Lines 12 and 13 close main and the class.
FeatureStatic methodInstance method
AssociationAssociated with the class.Associated with an object.
DeclarationIncludes the static modifier.Does not include static.
Invocation contextCan use the class name.Requires an object context.
Instance dataNeeds an object reference to access it.Can access its object's instance data directly.

The keyword static does not determine whether a method returns a value, receives parameters, or produces output. Those are separate properties. Likewise, creating an object is distinct from calling one of its ordinary member methods.

How are pure and impure methods distinguished?

A pure method produces its result from its supplied values without observable side effects. A side effect is an observable action beyond returning a result, such as changing an object's stored data or displaying output. An impure method has such effects or depends on changing external state.

For the same argument values, a pure calculation gives the same result. It can use local variables while carrying out that calculation. Changing a local accumulator does not by itself change data outside the method, so local assignments do not automatically make a method impure.

How should the worked methods be classified?

The calcpow method performs its calculation using number, power, result and i. It returns the product without changing an object's data or printing. For its stated input range, it is a pure calculation. The fact that result changes repeatedly inside the method does not alter that classification.

The displayed versions of addnum, fullname, sumSquares, incrValue and calcFact print output. They therefore have an observable side effect. In particular, incrValue leaves the caller's integer unchanged but still prints; this distinguishes parameter passing from purity.

QuestionWhat it establishes
Does the method return a value?Whether its call supplies a result to the caller.
Does it modify shared data or perform output?Whether it has those observable side effects.
Does its declaration include static?Whether it belongs to the class rather than requiring an instance context.
Does it have parameters?Whether arguments supply values through its parameter list.

Note: Returning a value does not prove that a method is pure. Check its complete body for changes to shared data, output, and dependence on changing external state. Likewise, static and pure are different classifications.

When explaining a classification, name the relevant action. Saying that a method prints a greeting identifies the side effect. Saying that it merely contains an assignment is insufficient, because assignments to local working variables and assignments to shared object data have different consequences.

How do method signatures and overloading work?

A method signature, for the ordinary methods considered here, consists of the method name and the ordered parameter types. A method prototype is a header-level description of the method's interface, meaning how it is called and what it returns. Do not confuse that description with the narrower Java signature.

Method overloading declares methods with the same name but different parameter lists. The lists may differ in number, types, or the order of different types. Overloading is a form of polymorphism, in which one name supports different method forms.

Rule: Changing only the return type does not create a distinct overload. Changing only parameter names does not change the signature either. The compiler, which checks and translates Java source, must distinguish the applicable parameter lists.

How can one overload call another?

In mixedFraction, num is the numerator and deno is the denominator of a fraction. The numerator is the amount divided; the denominator is the non-zero divisor. quotient stores the integer division result and remainder stores the part left over.

The operator /, used with integers, performs integer division; % finds the remainder. The operator != means not equal to. An if statement selects its block when a condition is true; else selects the alternative when it is false.

A mixed fraction combines a whole-number part and a proper fractional part. For positive integers, a proper fraction has its numerator smaller than its denominator. The worked inputs are positive integers. The two-parameter method uses the supplied denominator; the one-parameter method calls it with denominator 1. Java does not use a default parameter assignment in these method headers.

Worked example 8. Use overloaded methods for 17 divided by 2 and for 9 divided by 1.

class Functions {
static void mixedFraction(int num, int deno) {
int remainder = num % deno;
if (remainder != 0) {
int quotient = num / deno;
System.out.println(quotient + " (" + remainder + "/" + deno + ")");
} else {
System.out.println("The given fraction evaluates to a whole number");
}
}
static void mixedFraction(int num) {
mixedFraction(num, 1);
}
public static void main(String[] args) {
mixedFraction(17, 2);
mixedFraction(9);
}
}

Answer: The output, on separate lines, is:

8 (1/2)
The given fraction evaluates to a whole number

  1. Line 1 opens the class. Line 2 declares the overload with two int parameters.
  2. Line 3 calculates the remainder; line 4 tests whether it is non-zero.
  3. Line 5 calculates the whole-number part. Line 6 displays that part and the remaining fraction.
  4. Line 7 opens the alternative branch; line 8 displays the whole-number message.
  5. Line 9 closes that branch; line 10 closes the two-parameter method.
  6. Line 11 declares the one-parameter overload. Line 12 calls the two-parameter overload with denominator 1; line 13 closes it.
  7. Line 14 opens main. Line 15 selects the two-parameter overload for 17 and 2.
  8. Line 16 selects the one-parameter overload for 9. Lines 17 and 18 close main and the class.

The signatures are mixedFraction(int, int) and mixedFraction(int). Both methods return void, yet their argument counts distinguish them. The second call reaches both overloads: the one-parameter method delegates the operation to the two-parameter method.

When checking overloaded calls, first compare the method name and argument count, then the argument types and their order. Parameter names explain the variables' purposes but do not select an overload. The expected destination of a return value cannot rescue otherwise identical signatures.

Glossary

  • User-defined method — A named operation written by the programmer inside a class to perform a required task.
  • Modular programming — Dividing a program into smaller parts with distinct responsibilities that can be developed and reused.
  • Method header — The part declaring a method's modifiers, return type, name and formal parameter list.
  • Method body — The block of instructions enclosed in braces that runs when the method is invoked.
  • Actual parameter — An argument supplied in a method call whose evaluated value is passed to a parameter.
  • Formal parameter — A variable declared in the method header to receive a value supplied by a caller.
  • Return type — The declared type of a method's returned result, or void when no value is returned.
  • Pass by value — Supplying a copy of an argument's value to the called method's corresponding parameter variable.
  • Object reference — A value identifying an object, which Java can copy into a method parameter.
  • Static method — A method associated with its class that can be invoked using the class name.
  • Instance method — A non-static method invoked on an object and able to access that object's instance data.
  • Pure method — A method producing its result from supplied values without observable side effects or changing external dependencies.
  • Impure method — A method with observable side effects or a result dependent on changing external state.
  • Method signature — For ordinary methods here, the method name together with its ordered list of parameter types.
  • Method overloading — Declaring methods with the same name and different parameter lists within the relevant class structure.

Common errors and misconceptions

  • Misconception: Writing a method definition executes its body. Correct: A call invokes the method; the existence or position of its definition does not by itself run its instructions.
  • Misconception: Actual and formal parameter names must match. Correct: Argument position and compatible types determine the correspondence. The caller's variable and the formal parameter can have different names.
  • Misconception: Changing an integer parameter changes the caller's integer. Correct: Java supplies a copy of the value. Assigning to the parameter leaves the caller's separate variable unchanged.
  • Misconception: Object arguments prove that Java uses call by reference. Correct: Java copies the reference value. Shared object data may change, but parameter reassignment does not reassign the caller's reference variable.
  • Misconception: Displaying a calculated answer returns it. Correct: Printing produces output; return supplies a result to the caller. A void method cannot supply a value for an assignment.
  • Misconception: Every static method is pure. Correct: Static describes class association. Purity depends on the method's behaviour, including effects on shared data and output.
  • Misconception: A different return type is sufficient for overloading. Correct: The method name and ordered parameter types determine these signatures. Return type alone cannot distinguish overloads.
  • Misconception: Any instance method can be called directly from static main. Correct: Main needs an appropriate object reference to invoke an instance method. Object creation and method invocation are distinct operations.

Exam-style questions with model answers

Q1. Define a user-defined method and state one benefit of using it. [2 marks]
  1. A user-defined method is a named block of instructions written by the programmer within a class to perform a required task.
  2. It improves reusability because the same operation can be called repeatedly without rewriting its complete instructions at each calling position.
Q2. A method is declared as static void fullname(String first, String last). It is called as fullname("Gyan", "Vardhan"). Identify the actual parameters, formal parameters and correspondence between them. [3 marks]
  1. The actual parameters are the string literals "Gyan" and "Vardhan". They are the arguments supplied at the call, in that order.
  2. The formal parameters are first and last. Both are declared with type String in the method header to receive the supplied values.
  3. The first argument supplies first with "Gyan", and the second supplies last with "Vardhan". This matching follows position, rather than requiring identical argument and parameter names.
Q3. Inside one class, static void incrValue(int num) contains num = num + 5; followed by System.out.println(num);. In main, int number = 8; is followed by incrValue(number); and System.out.println(number);. State both output lines and explain the result. [4 marks]
  1. The first output line is 13. Inside incrValue, num initially receives the value 8 and the addition increases its local value by 5.
  2. The second output line is 8. This is printed from number in main after the method call has completed.
  3. Java passes the integer argument by value, supplying a copy of number's value to the formal parameter num.
  4. Assigning the increased value to num changes that parameter, not the caller's separate variable number. The void method returns no replacement value.
Q4. In class Functions, void calcFact(int num) is a non-static method. Static main contains Functions functions = new Functions(); followed by functions.calcFact(5);. Explain object creation, the reference variable, member selection and argument passing. [4 marks]
  1. The expression new Functions() creates a Functions object and invokes its constructor as part of object initialisation.
  2. The declaration stores the resulting object reference in functions. This variable has the class type Functions and identifies the created object.
  3. The dot in functions.calcFact(5) selects calcFact through that reference, providing the object context needed for the non-static method.
  4. The call supplies the integer value 5 to the formal parameter num. Main invokes the instance method through the object rather than through a direct unqualified call.
Q5. The pure method static int calcpow(int number, int power) initialises int result = 1;, repeats result = result * number; exactly power times, and returns result. Main evaluates int answer = calcpow(5, 4); and then prints answer. Explain the parameter values, calculation, returned result, assignment and output, and purity. [6 marks]
  1. The actual arguments are 5 and 4. Their values are supplied to the formal parameters number and power respectively, following their positions in the call.
  2. The result starts at 1 and is multiplied by 5 exactly four times, producing the product 5 * 5 * 5 * 5.
  3. The return statement supplies the resulting integer 625 to the caller and ends this execution of calcpow.
  4. The assignment in main stores that returned integer in answer, making the result available for later statements in the caller.
  5. Printing answer displays 625. The printing occurs in main, whereas calcpow supplies the value through return.
  6. The method's stated work uses its parameters and local result without observable side effects. Updating its local working value does not itself make the calculation impure.
Q6. A class defines static void mixedFraction(int num) and static void mixedFraction(int num, int deno). Explain why these are overloads, which is selected by mixedFraction(9), and whether changing only a return type or parameter name creates a new overload. [5 marks]
  1. The methods share the name mixedFraction but have different parameter counts. One accepts one int argument, while the other accepts two int arguments.
  2. Their signatures are mixedFraction(int) and mixedFraction(int, int). These distinct ordered parameter lists allow the declarations to coexist as overloads.
  3. The call mixedFraction(9) supplies one integer argument, so it selects the method with the single int parameter num.
  4. Changing only the return type would not create a new signature for either method. A return type difference alone cannot define an additional overload.
  5. Changing only a formal parameter's name would also leave its type and position unchanged. It therefore would not create a different signature or an additional overload.

Key takeaways

  • A user-defined method gives a task a reusable name, helping organise a program into smaller operations with clear responsibilities.
  • A method definition supplies the header and body; a method call transfers execution into that body and then back to the caller.
  • Actual parameters supply argument values at a call, while formal parameters receive those values in the corresponding positions.
  • Java passes values by copying them, including reference values; parameter reassignment therefore does not reassign the caller's variable.
  • Returning a value makes it available to the caller, whereas printing displays output without supplying that value as a result.
  • Static methods belong to the class, while instance methods operate with an object context supplied through an appropriate reference.
  • Purity concerns side effects and dependence on external state; it cannot be decided from static, void or a return type alone.
  • Overloaded methods share a name but have different parameter lists; changing only return types or parameter names is insufficient.

Test yourself

What is the difference between defining and invoking a method?

Defining supplies the method's header and instructions. Invoking calls it, causing those instructions to execute for that call.

Does a method with an empty parameter list necessarily perform no calculation?

No. It can calculate using values declared in its body or accessible data members, even though the call supplies no arguments.

In static void incrValue(int num), which part identifies the formal parameter and its type?

The declaration int num identifies num as the formal parameter and int as the type of value it receives.

Why does reassigning an object parameter not reassign the caller's reference variable?

The parameter holds a copied reference value. Reassigning that separate parameter changes its reference, rather than the caller's variable.

Can a method both return a value and be impure?

Yes. A method can return a result while also changing shared data or producing output, which are observable side effects.

What information distinguishes mixedFraction(int) from mixedFraction(int, int)?

The parameter lists have different counts: one integer parameter versus two. Their method names match, but their signatures differ.

Does changing a method's parameter names alter its signature?

No. For these methods, the signature uses the name and ordered parameter types, rather than the names assigned to parameters.

What must static main provide when it invokes a non-static method?

It must provide an appropriate object context, normally by calling the instance method through a variable that references an object.