Introduction
When most beginners start learning Java, they spend a lot of time understanding variables, data types, classes, and objects. While these concepts are important, there is another concept that gives life to an object: methods.
Imagine you have a mobile phone.
The phone contains data:
- Brand
- Model
- Storage
- Battery Percentage
But simply having data is not enough.
A phone can also perform actions:
- Make a call
- Send a message
- Open an application
- Take a picture
In Object-Oriented Programming (OOP), data is represented using variables, while actions are represented using methods.
Without methods, objects would simply hold information. They would not be able to perform any meaningful work.
According to Oracle's Java Documentation, a method is a collection of statements that perform a specific operation. In simple terms, a method is a reusable block of code that performs a particular task.
In this article, we will take a complete journey through:
- Objects and non-static variables
- Accessing methods through objects
- Understanding what methods are
- Learning every keyword used in method creation
- Return types and the
returnkeyword - Parameters and arguments
- Common compile-time errors
- Method Overloading
- Compile-Time Polymorphism
By the end of this article, you will understand not only how methods work but also why Method Overloading is called Compile-Time Polymorphism.
Let's begin from the very beginning.
Objects Access Non-Static Variables
Before understanding methods, we must understand how objects interact with variables.
Consider the following class:
class Student {
String name = "Rahul";
}
Here:
String name = "Rahul";
is called an instance variable or non-static variable.
Official Definition
According to Oracle Java Documentation, instance variables belong to an object instance. Every object created from a class gets its own copy of instance variables.
Beginner-Friendly Explanation
Think of a class as a blueprint and an object as the real thing created from that blueprint.
Example:
- Class → Student
- Object → Rahul, Priya, Arjun
Every student object can have its own name.
To access an instance variable, we need an object.
public class Main {
public static void main(String[] args) {
Student s = new Student();
System.out.println(s.name);
}
}
Output:
Rahul
Notice:
s.name
This follows the pattern:
object.variable
The object s is being used to access the variable name.
Common Beginner Error
System.out.println(name);
Error:
cannot find symbol
Why?
Because name belongs to the object, not directly to the main method.
Fix:
System.out.println(s.name);
Key takeaway:
Non-static variables are accessed using
object.variable.
Objects Can Also Access Methods
Objects do not only access variables.
They can also access methods.
Let's create a simple method.
class Student {
void study() {
System.out.println("Student is studying");
}
}
Create an object:
Student s = new Student();
Call the method:
s.study();
Output:
Student is studying
This follows the pattern:
object.method();
Real-World Analogy
Imagine a TV remote.
When you press a button, an action happens.
Similarly:
s.study();
asks the object to perform the behavior called study().
Methods are often called the behavior of an object.
What Happens If the Method Does Not Exist?
Suppose we write:
Student s = new Student();
s.play();
But inside Student:
class Student {
void study() {
}
}
There is no play() method.
Error:
cannot find symbol
symbol: method play()
location: variable s of type Student
Why Does This Error Occur?
Java checks everything before running the program.
The compiler asks:
- Does the class exist?
- Does the object exist?
- Does the method exist?
Since play() cannot be found, compilation fails.
Important Rule:
A method must be declared before it can be called.
What Exactly Is a Method?
Oracle Documentation defines a method as:
A collection of statements grouped together to perform an operation.
Beginner-Friendly Definition
A method is a reusable block of code that performs a specific task.
Examples:
- Calculating salary
- Printing information
- Finding the largest number
- Sending an email
Without methods:
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
With methods:
greet();
greet();
greet();
Methods help achieve:
- Reusability
- Readability
- Maintainability
- Modularity
Creating Your First Method
Example:
public void printActivity() {
System.out.println("Learning Java");
}
This method prints a message.
But what does each keyword mean?
Let's break it down.
Understanding Every Keyword in a Method
public
public
Official Meaning
An access modifier that makes a member accessible from anywhere.
Beginner-Friendly Meaning
Anyone can use this method.
Think of it as an open door.
Example:
public void printActivity()
means:
"This method can be accessed by other classes."
void
void
Official Meaning
The method returns no value.
Beginner-Friendly Meaning
The method performs work but gives nothing back.
Example:
void greet() {
System.out.println("Hello");
}
It prints something but does not return any value.
printActivity
printActivity
This is the method name.
Java uses the method name to identify which method to execute.
Good method names should describe the action being performed.
Examples:
calculateSalary()
sendEmail()
printActivity()
Bad examples:
abc()
xyz()
doit()
Parentheses ()
()
Used to hold parameters.
Currently:
()
means no parameters.
Curly Braces {}
{
}
The method body.
The actual statements execute inside these braces.
Return Types in Java
Every method must specify what type of value it returns.
Example:
public int getAge() {
return 25;
}
Here:
int
means the method returns an integer.
Why Does Java Need a Return Type?
Before compilation, Java must know:
"What kind of value will this method send back?"
That's why return types are mandatory.
Error: Missing Return Type
Wrong:
public getAge() {
return 25;
}
Error:
invalid method declaration; return type required
Why?
Java does not know what type of value will be returned.
Fix:
public int getAge() {
return 25;
}
Understanding the return Keyword
The keyword:
return
sends a value back to the caller.
Example:
public int getAge() {
return 25;
}
Usage:
int age = getAge();
System.out.println(age);
Output:
25
Flow:
Method Call
↓
Method Executes
↓
return 25
↓
Caller Receives 25
Why Do We Use void?
Consider:
void greet() {
System.out.println("Hello");
}
The method simply performs an action.
Nothing needs to be returned.
Therefore:
void
is used.
Rule:
Do work only → void
Return value → int, String, double, boolean, etc.
Parameters and Arguments
This is one of the most commonly confused topics.
Parameter
void greet(String name)
name is a parameter.
Argument
greet("Rahul");
"Rahul" is an argument.
Simple rule:
Method Definition → Parameters
Method Call → Arguments
Think of parameters as placeholders and arguments as actual values.
No-Argument Method
Method:
void greet() {
System.out.println("Hello");
}
Call:
greet();
Output:
Hello
Error: Passing an Argument to a No-Argument Method
Method:
void greet() {
}
Call:
greet("Rahul");
Error:
method greet cannot be applied to given types
Fix:
void greet(String name)
One-Parameter Method
Method:
void greet(String name) {
System.out.println("Hello " + name);
}
Call:
greet("Rahul");
Output:
Hello Rahul
Error: Missing Required Argument
Method:
void greet(String name)
Call:
greet();
Error:
method greet cannot be applied to given types
Why?
The method expects one argument but receives none.
Fix:
greet("Rahul");
Two-Parameter Method
Method:
void add(int a, int b) {
System.out.println(a + b);
}
Call:
add(10, 20);
Output:
30
Error: Passing One Argument Instead of Two
Call:
add(10);
Error:
method add cannot be applied to given types
Fix:
add(10, 20);
String Parameters
Method:
void display(String name) {
System.out.println(name);
}
Correct:
display("Java");
Output:
Java
Error: Passing Wrong Data Type
Method:
display(String name);
Call:
display(100);
Error:
incompatible types
Reason:
Method expects:
String
but receives:
int
Fix:
display("100");
Why Method Overloading Exists
Imagine a printing system.
Without overloading:
printInt()
printString()
printDouble()
printTwoNumbers()
Too many method names.
Difficult to remember.
Java provides a cleaner solution.
Method Overloading
Official Definition
Method Overloading is the ability to define multiple methods with the same name but different parameter lists.
Example:
class Printer {
void print() {
System.out.println("No Data");
}
void print(int num) {
System.out.println(num);
}
void print(String text) {
System.out.println(text);
}
void print(int a, int b) {
System.out.println(a + b);
}
}
Calls:
print();
print(10);
print("Java");
print(10, 20);
Output:
No Data
10
Java
30
Notice:
Same method name.
Different parameter lists.
That is Method Overloading.
Rules of Method Overloading
Valid:
print()
print(int)
print(String)
print(int, int)
Invalid:
int print()
String print()
Changing only the return type is not overloading.
The parameter list must be different.
What Is Compile-Time Polymorphism?
The word polymorphism comes from:
Poly = Many
Morph = Forms
Meaning:
One Name
Many Forms
Example:
print()
print(int)
print(String)
print(int, int)
All methods share the same name.
But each has a different form.
Why Is Method Overloading Called Compile-Time Polymorphism?
Consider:
print(10);
During compilation, Java checks:
void print(int num)
For:
print("Java");
Java checks:
void print(String text)
The decision happens before the program runs.
That means the compiler determines which method should execute.
Therefore:
Method Overloading
↓
Method Selected During Compilation
↓
Compile-Time Polymorphism
It is also called:
Static Polymorphism
because the method resolution happens during compile time rather than runtime.
Final Summary
In this article, we started with the fundamentals of objects and non-static variables. We learned how objects access data using the dot operator and how methods represent the behavior of an object.
We explored what methods are, why they exist, and how they help us write reusable and maintainable code. We broke down every component of a method, including public, void, method names, parentheses, and method bodies.
We then moved into return types and the return keyword, understanding how methods can send values back to the caller. After that, we learned the difference between parameters and arguments and examined common compile-time errors that beginners frequently encounter.
Finally, we reached Method Overloading. Instead of creating many methods with different names, Java allows us to use the same method name for multiple operations by changing the parameter list.
This feature is called Method Overloading, and because Java determines the correct method during compilation, it is known as Compile-Time Polymorphism.
The most important takeaway from this entire journey is:
Method Overloading allows one method name to have multiple forms, and Java selects the correct form during compilation. This ability is what makes Method Overloading an example of Compile-Time Polymorphism.
Understanding methods thoroughly is one of the strongest foundations you can build as a Java developer. Almost every Java application—from simple console programs to enterprise-level systems—relies heavily on methods. Mastering them today will make advanced topics such as inheritance, runtime polymorphism, interfaces, and design patterns much easier to understand in the future.
Happy Coding!
References
Oracle Java Tutorials – Methods
https://docs.oracle.com/javase/tutorial/java/javaOO/methods.htmlOracle Java Tutorials – Objects
https://docs.oracle.com/javase/tutorial/java/javaOO/objects.htmlJava Language Specification – Methods
https://docs.oracle.com/javase/specs/Oracle Java Tutorials – Passing Information to a Method or Constructor
https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
Top comments (0)