1. Java Methods
A function is a block of statements that should return a value. A method is a block of code that runs when it is called.
- A method is a set of code referred to by name and can be called (invoked) at any point in a program by using the method's name.
- Data can be passed into a method as parameters.
- Methods are used to perform certain actions and are also known as functions.
- A method can be thought of as a subprogram that acts on data and often returns a value.
- Each method has its own name.
- When the method name is encountered, program execution branches to the body of that method.
- When the method finishes, execution returns to the point from which it was called and continues with the next line.
2. Advantages of Methods
- Simple to write.
- Easy to read, write and debug.
- Easy to maintain or modify.
- Small functions tend to be self-documenting and highly readable.
- A function can be called any number of times and from any place with different parameters.
- Methods support code reuse: define the code once and use it many times.
3. Create a Method
A method must be declared within a class. It is defined using the method name followed by parentheses ().
Java provides predefined methods such as System.out.println(), but programmers can also create their own methods to perform specific actions.
Basic Example from the Source
public class demo() {
sop("Hello Welcome to Java method concepts");
}
4. Signature of a Function / Defining a Method
The source presents the basic structure of a method as:
Access Specifier
Access Modifier
return type
function name
(args list)
{
// method body
}
| Part | Purpose |
|---|---|
| Access Specifier | Controls accessibility of the method. |
| Access Modifier | Defines additional method behavior or characteristics. |
| Return Type | Specifies the type of value returned by the method. |
| Function / Method Name | Name used to invoke the method. |
| Arguments List | Parameters received by the method. |
| Method Body | Statements executed when the method is invoked. |
5. Declaring a Method
The source gives the following general method declaration form:
AccessSpecifier AccessModifier return_type methodName(arguments/parameters);
6. What Is Actually Passed to a Method?
Actual Parameters
The values or variables passed while calling a function are known as actual parameters.
Formal Parameters
The variables written or declared in the function definition/prototype are called formal parameters. They receive their values when the method is called.
Example
int number1 = 25;
int number2 = 47;
int sum = add(number1, number2); // actual parameters
public int add(int x, int y) { // formal parameters
return (x + y);
}
7. Method Example
The source demonstrates invoking a method and defining a method that receives two integer parameters.
// Invoke (call) the method
int number1 = 25;
int number2 = 47;
int sum = add(number1, number2);
// Method definition
public int add(int x, int y)
{
return (x + y);
}
| Term | Example |
|---|---|
| Actual Parameters | number1, number2 |
| Formal Parameters | x, y |
| Return Value | x + y |
8. Demo Program for Methods
import java.io.*;
import BreezyGUI.*;
public class DisplayClass
{
public static void main(String[] args)
{
System.out.println("Calling all methods!");
asterisks(); // invoking method
System.out.println("Great job, method!");
asterisks(); // invoking method again
System.out.println("We're outta here!");
}
// Method for asterisks
public static void asterisks()
{
int count;
for(count = 1; count <= 20; count++)
System.out.print("*");
System.out.println();
}
}
Output
Calling all methods! ******************** Great job, method! ******************** We're outta here!
9. Pass-by-Value
The source explains that formal parameter names do not have to be the same as actual parameter names, but their data types must correspond.
With pass-by-value, when a method is called, a copy of the value of each argument is passed to the method. The copied value can be changed inside the method, but such a change has no effect on the original argument.
Example
int number1 = 25;
int number2 = 47;
int sum = add(number1, number2);
public int add(int x, int y)
{
return (x + y);
}
10. JVM Architecture
The source introduces JVM architecture in the context of how Java methods and classes are handled at runtime.
Main Components Shown in the Source Diagram
Class Loader
Reads the .class file and saves bytecode in the method area.
Method Area
Shared JVM area containing class-level information for each class file.
Heap
JVM memory area where objects are allocated. The JVM creates a Class object for each .class file.
Stack
JVM memory area used for temporary variables.
PC Registers
Keep track of the instruction executed and the instruction to be executed.
Native Method Stack
Provides runtime data areas used by native methods.
Native Method Interface
Enables Java code to call or be called by native applications.
Native Method Libraries
Libraries used by native methods.
Execution Engine
Executes the bytecode loaded into the JVM.
11. JVM Memory Areas
| Area | Description |
|---|---|
| Method Area | There is one method area in a JVM, shared among all classes. It holds class-level information of each class file. |
| Heap | Part of JVM memory where objects are allocated. |
| Stack | Part of JVM memory used for storing temporary variables. |
12. PC Registers
The PC Register keeps track of which instruction has been executed and which instruction is going to be executed.
13. Native Method Areas
Native Method Stack
A native method can access the runtime data areas of the virtual machine.
Native Method Interface
Enables Java code to call or be called by native applications. Native applications are programs specific to the hardware and operating system of a system.
Native Method Libraries
Libraries used to support native method execution.
14. Garbage Collection
The source explains that a class instance is explicitly created by Java code and, after use, is automatically destroyed by garbage collection for memory management.
15. Quick Revision
| Topic | Key Point |
|---|---|
| Function | A block of statements that should return a value. |
| Method | A named block of code that executes when it is called. |
| Parameters | Data passed into a method. |
| Method Reuse | Define code once and invoke it multiple times. |
| Method Signature | Access specifier, access modifier, return type, method name and argument list. |
| Actual Parameters | Values or variables supplied during a method call. |
| Formal Parameters | Variables declared in the method definition that receive argument values. |
| Pass-by-Value | A copy of an argument's value is passed to the method. |
| Class Loader | Loads .class files and saves bytecode in the method area. |
| Method Area | Shared JVM area containing class-level information. |
| Heap | JVM memory area where objects are allocated. |
| Stack | JVM memory area used for temporary variables. |
| PC Register | Tracks current and next instructions for a thread. |
| Native Method Stack | Runtime data area accessible by native methods. |
| Native Method Interface | Allows Java code to call or be called by native applications. |
| Garbage Collection | Automatic memory management for unused objects. |