[Go to site: main page, start]

0% found this document useful (0 votes)
6 views8 pages

Methods in Java

Java methods are blocks of code that perform specific tasks and are essential for code reusability, organization, and maintainability. Methods are categorized into predefined and user-defined types, and they can be called using objects or directly if they are static. The document also explains the method call stack, syntax, and best practices for naming and using methods in Java.

Uploaded by

sabiya fatima
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Methods in Java

Java methods are blocks of code that perform specific tasks and are essential for code reusability, organization, and maintainability. Methods are categorized into predefined and user-defined types, and they can be called using objects or directly if they are static. The document also explains the method call stack, syntax, and best practices for naming and using methods in Java.

Uploaded by

sabiya fatima
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2/6/26, 12:20 PM Java Methods - GeeksforGeeks

Tutorials
Search... Practice
Sign In
Jobs

Java Methods
Last Updated : 24 Jan, 2026

Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving
both efficiency and organization. All methods in Java must belong to a class. Methods are similar to
functions and expose the behavior of objects.

A method allows to write a piece of logic once and reuse it wherever needed in the program.
This helps keep your code clean, organized, easier to understand and manage.

public class Geeks


{
// An example method
public void printMessage() {
[Link]("Hello, Geeks!");
}

public static void main(String[] args) {

// Create an instance of the class


// containing the method
Geeks obj = new Geeks();

// Calling the method


[Link]();
}
}

Output

Hello, Geeks!

Explanation:

Here, first we create a method which prints Hello, Geeks!


printMessage() is a simple method that prints a message.
It has no parameters and does not return anything.

Syntax of a Method
l Advanced Java Interview Questions Exercises Examples Quizzes Projects Cheatsheet DSA in Java Jav Sign In

[Link] 1/8
2/6/26, 12:20 PM Java Methods - GeeksforGeeks

Key Components of a Method Declaration

Method consists of a modifier (Define access level), return type (what value returned or void), name (Define
the name of method follows camelCase), parameters (optional inputs), and a body (Write your logic here).

Why Use Methods?

Breaking code into separate methods helps improve readability, reusability, and maintainability

Code Reusability: Write once, use multiple times without repeating code so that code reusability
increase.

[Link] 2/8
2/6/26, 12:20 PM Java Methods - GeeksforGeeks

Modularity: Dividing a program into separate methods allows each method to handle a specific task,
making the code more organized and easier to manage.
Readability: Smaller, named methods make the code easier to read and understand.
Maintainability: It’s easier to fix bugs or update code when it's organized into methods.

Method Call Stack in Java


Java is an object-oriented and stack-based programming language where methods play a key role in
controlling the program's execution flow. When a method is called, Java uses an internal structure known
as the call stack to manage execution, variables, and return addresses.

What is the Call Stack

The call stack is a data structure used by the program during runtime to manage method calls and local
variables. It operates in a Last-In-First-Out (LIFO) manner, meaning the last method called is the first one to
complete and exit.

How Are Methods Executed

When a method is called:

A new stack frame is added to the call stack to store method details.
The method runs its code.
After execution, the stack frame is removed, and control goes back to the calling method.

Java automatically manages the call stack using the Java Virtual Machine (JVM).

Let's use an image to understand how the method call stack works

1/9

Example: Let’s understand how the above image works with the help of a Java code example

public class CallStackExample {

public static void D() {


float d = 40.5f;
[Link]("In Method D");
}

[Link] 3/8
2/6/26, 12:20 PM Java Methods - GeeksforGeeks
public static void C() {
double c = 30.5;
[Link]("In Method C");
}

public static void B() {


int b = 20;
C(); // Calling C
[Link]("In Method B");
}

public static void A() {


int a = 10;
B(); // Calling B
[Link]("In Method A");
}

public static void main(String[] args) {


A(); // Start with function A
D(); // Then call D
}
}

Output

In Method C
In Method B
In Method A
In Method D

Types of Methods in Java

1. Predefined Method

Predefined methods are the method that is already defined in the Java class libraries. It is also known as the
standard library method or built-in method. For example, random() method which is present in the Math
class and we can call it using the [Link]() as shown in the below example.
Example:

[Link]() // returns random value


[Link] // return pi value

2. User-defined Method

The method written by the user or programmer is known as a user-defined method. These methods are
modified according to the requirement.
Example:

sayHello // user define method created above in the article


Greet()
setName()

Different Ways to Create Java Method


There are two ways to create a method in Java:

1. Instance Method: Access the instance data using the object name. Declared inside a class.

[Link] 4/8
2/6/26, 12:20 PM Java Methods - GeeksforGeeks

Example:

// Instance Method
void method_name() {
// instance method body
}

2. Static Method: Access the static data using class name. Declared inside class with static keyword.
Example:

// Static Method
static void method_name() {
// static method body
}

Method Signature
It consists of the method name and a parameter list.

Number of parameters
Type of the parameters
Order of the parameters

Note: The return type and exceptions are not considered as part of it.

Method Signature of the above function:

max(int x, int y) Number of parameters is 2, Type of parameter is int.

Naming a Method
In Java language method name is typically a single word that should be a verb in lowercase or a multi-
word, that begins with a verb in lowercase followed by an adjective, noun. After the first word, the first
letter of each word should be capitalized.
Rules to Name a Method:
Method names must start with a verb in lowercase.
Multi-word names should follow camelCase format.
Method names should be unique within a class unless method overloading is allowed in Java.

Calling Different Types of Methods in Java


Method calling in Java means invoking a method to execute the code it contains. It transfers control to the
process, runs its logic, and then returns to the calling point after execution.

1. Calling a User-Defined Method

User-defined methods are blocks of code written by the programmer. To execute a user-defined method,
we first create an object of the class (if the method is non-static) and then call the method using that object.
Example:

class Geeks {
void hello() {
[Link]("This is a user-defined method.");
}

[Link] 5/8
2/6/26, 12:20 PM Java Methods - GeeksforGeeks
public static void main(String[] args) {
Geeks obj = new Geeks(); // Create object
[Link](); // Call method
}
}

Output

This is a user-defined method.

Explanation: In the above code a class Geeks with a method hello() that displays a message. In the main()
method, we create an object of the class and use it to call the hello() method.

2. Calling an Abstract Method

Abstract methods have no body and must be overridden in a subclass. They are called using an object of
the subclass.

Example 2: Calling Methods in Different Ways

abstract class GeeksHelp {


abstract void check(String name); // Abstract method
}

public class Geeks extends GeeksHelp {


@Override
void check(String name) {
[Link](name);
}

public static void main(String[] args) {


Geeks obj = new Geeks(); // Subclass object
[Link]("GeeksforGeeks");
}
}

Output

GeeksforGeeks

Explanation: In the above code an abstract class GeeksHelp with an abstract method check(), which is
implemented in the subclass Geeks. In the main() method, an object of Geeks is created to call the
implemented check() method.

3. Calling the Predefined Methods

Java provides many built-in methods via the Java Standard Library, like hashCode().

public class Geeks {


public static void main(String[] args) {
Geeks obj = new Geeks();
[Link]([Link]()); // Predefined method
}
}

[Link] 6/8
2/6/26, 12:20 PM Java Methods - GeeksforGeeks

Output

1510467688

Explanation: In the above code an object of the Geeks class and calls the predefined hashCode() method. It
prints a unique integer value representing the object's memory address hash.

4. Calling a Static Method

Static methods belong to the class, not the object. They can be called without creating an object.

class Test {
static void hello() {
[Link]("Hello");
}
}
public class Geeks {
public static void main(String[] args) {
[Link](); // Call static method directly
}
}

Output

Hello

Explanation: we call a static method hello() from the test class without creating an instance of the class.
The method prints "Hello" when invoked from the main method.

Related Posts:

Abstraction
Instance Method
Static Method
Functions in
Java

Applications
of Functions.

Functions in Java Visit Course

[Link] 7/8
2/6/26, 12:20 PM Java Methods - GeeksforGeeks

Suggested Quiz 5 Questions

Which of the following is true about methods in Java?

A A method must return a value.

B Methods can only be static.

C Methods can be parameterized.


Company Explore Tutorials Courses Offline Preparation
About Us POTD Programming ML and Data Centers Corner
D Methods cannot have the same name in a class.
Corporate & Communications Address: Legal Practice Languages Science Noida Interview
Privacy Problems DSA DSA and Bengaluru Corner
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Policy Connect Web Placements Pune Aptitude
Pradesh (201305) Careers Blogs Technology Web Hyderabad Puzzles
Login to View Explanation
Registered Address:
Contact Us 190%
/5 AI, ML & Development < Previous GfG
Kolkata Next160>
Corporate Refund Data Science Data Science System Design
K 061, Tower K, Gulshan Vivante Solution on DevOps Programming
Apartment, Sector 137, Noida, Gautam
Campus Courses CS Core Languages
Buddh Nagar, Uttar Pradesh, 201305
Training Subjects DevOps &
Comment K kartik Program GATE Cloud 321
School GATE
Subjects Trending
Article Tags: Java java-basics Software and Technologies
Tools

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

[Link] 8/8

You might also like