Methods in Java: Simple Guide with Rules and Easy Examples
Every Java program is built from methods. They are the actions your objects can perform, the verbs in your code. If classes are nouns, methods are verbs. In this guide, I will explain what methods are, the rules for writing them, the different types, and how to declare them correctly — all in plain words with examples you can run in your IDE.
What is a method in Java?
A method is a set of code or instructions that performs a specific operation. It is used to represent the behavior of an object.
Think of a class like a blueprint for a car. The car has properties — color, speed, model. Those are fields. But the car also does things — it starts, it stops, it accelerates. Those actions are methods.
A method must be declared inside a class. It is defined with a name followed by parentheses (). Inside the parentheses, you can pass input values called parameters. Inside the curly braces, you write the code that does the work.
Here is a simple example:
class Hello {
void show() {
// write code here...
}
}
Here, show is the method name. void is the return type, meaning this method returns nothing. The empty parentheses mean it takes no input. The curly braces hold the code that runs when you call this method.
To call this method, you would create an object of Hello and call hello.show(). That is how methods are used.
Rules for writing a method
Java has clear rules about how a method must be written. If you break these rules, the compiler will complain. Here are the rules in simple words:
- A method must have an access modifier — like
private,default,protected, orpublic. This decides who can call the method. - A method can optionally use non-access modifiers — like
static,final,abstract,synchronized, and more. These change how the method behaves. - A method must have a return type — even if it returns nothing, you write
void. - A method must have a name — also called an identifier. This is how you call the method.
- Method names should be in camel case — starting with a lowercase letter, like
add(),addTwoNumber(),getStudentName().
If you follow these rules, your method will compile. If you break any of them, you will get a compile-time error.
A note on camel case
Camel case means the first word is lowercase and each following word starts with a capital letter. Like addTwoNumber — the "a" is lowercase, the "T" and "N" are uppercase. It looks like the humps of a camel. This is the standard convention in Java, and following it makes your code easier for others to read.
Two types: parameterized and non-parameterized
Methods come in two basic flavors based on whether they take input:
1. Non-parameterized method
This type takes no input. The parentheses are empty.
void show() {
// no input, just does its thing
}
Use this when the method does not need any data from outside to do its job. For example, a method that prints a welcome message.
2. Parameterized method
This type takes input values inside the parentheses. Each input is called a parameter.
void add(int i, int j) {
// i and j are parameters
}
Use this when the method needs data to work with. For example, a method that adds two numbers needs to know which numbers to add.
The rule is simple: when you get input from the user, use a parameterized method. When you do not get input, use a non-parameterized method.
Can a method have many parameters?
Yes. You can have as many parameters as you need, separated by commas. Like void printInfo(String name, int age, float marks). But if you find yourself passing more than four or five parameters, it might be a sign that your method is doing too much.
Static vs non-static methods
There is another way to divide methods: static and non-static. This is about whether the method belongs to the class or to individual objects.
Static method
A static method belongs to the class itself, not to any particular object. You can call it without creating an object. You mark it with the static keyword.
static void show() {
// belongs to the class
}
You would call this as ClassName.show(). No object needed.
Non-static method
A non-static method belongs to each object separately. You must create an object before you can call it.
void add(int i, int j) {
// belongs to the object
}
You would call this as objectName.add(5, 3). The object is required.
When to use each
- Use static when you want to share a method from different objects of the same class. The method does not depend on any object's data.
- Use non-static when you do not want to share a method from different objects of the same class. The method uses the object's fields and behavior.
For example, a method that adds two numbers passed as parameters can be static — it does not need any object's data. But a method that returns a student's name from the student object must be non-static — it needs to know which student object you are talking about.
Return types: primitive, non-primitive, and void
Every method has a return type. It tells Java what kind of value the method gives back to the caller.
Primitive return type
Methods can return any primitive type: int, float, double, boolean, char, and so on.
public int add(int i, int j) {
return i + j;
}
This method returns an int. The caller can use that value, store it in a variable, or pass it to another method.
Non-primitive return type
Methods can also return objects — any class type, including your own custom classes.
public Dog getDog() {
return new Dog();
}
Here, the return type is Dog, which is a non-primitive type. The method creates a new Dog object and returns it.
Void return type
If the method returns nothing, you use void as the return type. It means "this method does something but gives nothing back."
void display() {
// does something but returns nothing
}
You cannot use return value; inside a void method. You can use a plain return; to exit early, but you cannot return a value.
The rule is: if the method produces a value that the caller needs, give it a return type. If it just performs an action, use void.
Valid method declarations you can use
Java allows many combinations of modifiers, return types, and names. Here are the most common valid declarations. All of these compile without errors.
Basic definitions
// public method
public void show() {
}
// private method
private void show() {
}
// default access (no modifier)
void show() {
}
// protected method
protected void show() {
}
The access modifier decides who can call the method. public means anyone. private means only this class. protected means this class, subclasses, and the same package. Default (no modifier) means the same package only.
With non-access modifiers
// final method — cannot be overridden
public final void show() {
}
// static method — belongs to the class
public static void show() {
}
// final and static together
public final static void show() {
}
The order of modifiers does not matter. public final static and static public final are both valid. But the convention is to put access modifier first, then non-access modifiers.
Abstract method declaration
// abstract method — no body, must be overridden
public abstract void show();
An abstract method has no curly braces and no code. It ends with a semicolon. It can only exist inside an abstract class or an interface. Subclasses must provide the implementation.
Method with parameters and return type
// returns int, takes two int parameters
public int add(int i, int j) {
return i + j;
}
This is the most common form you will write. Access modifier, return type, name, parameters, body.
Common questions
What is a method in Java in simple words?
A method in Java is a set of code or instructions that performs a specific operation. It represents the behavior of an object and must be declared inside a class with a name followed by parentheses.
What are the rules for writing a method in Java?
A method must have an access modifier like public or private, an optional non-access modifier like static or final, a return type, and a name written in camel case. The name should describe what the method does.
What is the difference between parameterized and non-parameterized methods?
A parameterized method takes input values inside its parentheses, like void add(int i, int j). A non-parameterized method takes no input, like void show(). Use parameterized when you need to pass data, non-parameterized when you do not.
What is the difference between static and non-static methods?
A static method belongs to the class and can be shared by all objects of that class. A non-static method belongs to each object separately. Use static when the method does not depend on object state, and non-static when it does.
Can a method return any type of data in Java?
Yes, a method can return any type of data — primitive types like int, float, boolean, or non-primitive types like String, arrays, or custom objects like Dog. If the method returns nothing, use the void return type.
What is the correct way to name a method in Java?
Method names should be written in camel case, starting with a lowercase letter. For example, add(), addTwoNumber(), getName(). The name should be a verb or verb phrase that describes what the method does.
Where to go next
You now know what methods are, the rules for writing them, the difference between parameterized and non-parameterized methods, static and non-static methods, and all the valid return types. You have seen many valid declarations and understand when to use each one.
If you remember only one thing, remember this: A method is the behavior of an object. Name it like a verb, give it a clear return type, and keep it focused on one job.
The real learning happens when you write methods yourself. Open your IDE and create a class. Write a method that adds two numbers and returns the result. Write a method that prints a message and returns nothing. Write a static method and call it without an object. That is how the concept sticks.
If this helped, bookmark it. Next, try method overloading — writing multiple methods with the same name but different parameters. That is the natural next step.
2 comments:
Thank you sir For This Blog,It's really Helpful.
I Really Appriciate your Work .
guru ji apke blog was awesome
Post a Comment