Curriculum
Method Declaration and Definition in Java are important concepts used to create reusable blocks of code. Methods help developers organize programs into smaller logical units, making applications easier to understand, maintain, and debug.
In this Core Java course in Jaipur, students will learn how methods work in Java, method declaration syntax, method definition, method calling, parameters, return types, and practical examples used in real-world software development.
Methods are widely used in:
Understanding methods in Java helps students write modular and efficient Java programs.
A method in Java is a block of code that performs a specific task.
Methods help developers:
Instead of writing the same code repeatedly, developers can create methods and call them whenever needed.
Methods provide several benefits:
A Java method mainly contains:
returnType methodName(parameters) {
// method body
}
class MethodExample {
void display() {
System.out.println("Welcome to Java Methods");
}
public static void main(String[] args) {
MethodExample obj = new MethodExample();
obj.display();
}
}
Welcome to Java Methods
void
indicates method does not return any value.
display()
is the method name.
Code inside curly braces:
{
System.out.println("Welcome to Java Methods");
}
defines method functionality.
Methods execute only when called.
Example:
obj.display();
Java mainly provides:
Methods created by developers.
Example:
void calculate() {
}
Methods already available in Java libraries.
Example:
System.out.println();
Parameters allow methods to accept input values.
class Addition {
void add(int a, int b) {
int result = a + b;
System.out.println(result);
}
public static void main(String[] args) {
Addition obj = new Addition();
obj.add(10, 20);
}
}
30
Here:
int a, int b
are parameters.
Methods can return values using return statement.
class ReturnExample {
int square(int number) {
return number * number;
}
public static void main(String[] args) {
ReturnExample obj = new ReturnExample();
int result = obj.square(5);
System.out.println(result);
}
}
25
The return statement:
Methods can use access modifiers:
Example:
public void show() {
}
Static methods belong to class rather than objects.
class StaticMethod {
static void display() {
System.out.println("Static Method");
}
public static void main(String[] args) {
display();
}
}
Methods handle:
Methods process:
Methods manage:
Methods handle:
Methods do not execute automatically.
Incorrect:
int display() {
System.out.println("Hello");
}
Methods with return type must return value.
Passing wrong arguments causes errors.
| Method Declaration | Method Definition |
|---|---|
| Method signature | Actual implementation |
| Contains method name | Contains method body |
Understanding methods helps students:
In this lesson, students learned:
These concepts are essential for Java programming and software development.
A method is a reusable block of code used to perform a specific task.
Methods improve code reusability, readability, and maintainability.
Method declaration defines method name, return type, and parameters.
Method definition contains actual implementation of method.
return sends value back from method to caller.
Static methods belong to class, while non-static methods belong to objects.
WhatsApp us