codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

27 / 65 topics
24Java OOP25Java Classes/Objects26Java Class Attributes27Java Class Methods28Java Constructors29Java Modifiers30Java Encapsulation31Java Packages / API
Tutorials/Java Programming/Java Class Methods
☕Java Programming

Java Class Methods

Updated 2026-05-12
20 min read

Java Class Methods

In the previous topic, we explored how to define attributes (fields) in a Java class. Now, let's delve into methods, which are essential for defining the behaviors of objects. Methods allow classes to perform actions and interact with other classes or their environment.

Understanding methods is crucial because they encapsulate the functionality of a class, making your code modular, reusable, and easier to maintain.

Defining Methods

A method in Java is a block of code that performs a specific task. It can take inputs (parameters) and return outputs. Here's the basic syntax for defining a method:

Java
1returnType methodName(parameterList) {
2 // Method body
3};
  • returnType: The type of value the method returns. If it doesn't return anything, use void.
  • methodName: The name of the method.
  • parameterList: A list of parameters separated by commas. Each parameter has a type and a name.

Example: A Simple Method

Let's create a class with a simple method that adds two numbers:

Adder.java
1public class Adder {
2 // Method to add two integers
3 public int add(int a, int b) {
4 return a + b;
5 }
6
7 public static void main(String[] args) {
8 Adder calculator = new Adder();
9 int result = calculator.add(5, 3);
10 System.out.println("The sum is: " + result);
11 }
12}
Output
The sum is: 8

In this example:

  • We define a method add that takes two integers as parameters and returns their sum.
  • In the main method, we create an instance of Adder, call the add method, and print the result.

Calling Methods

To call a method, you need to use the following syntax:

Java
1objectReference.methodName(parameterList);
  • objectReference: A reference to an object of the class that contains the method.
  • methodName: The name of the method.
  • parameterList: The arguments passed to the method.

Example: Calling a Method

Continuing from the previous example, let's call the add method multiple times:

Adder.java
1public class Adder {
2 // Method to add two integers
3 public int add(int a, int b) {
4 return a + b;
5 }
6
7 public static void main(String[] args) {
8 Adder calculator = new Adder();
9
10 int result1 = calculator.add(5, 3);
11 System.out.println("The sum of 5 and 3 is: " + result1);
12
13 int result2 = calculator.add(10, 7);
14 System.out.println("The sum of 10 and 7 is: " + result2);
15 }
16}
Output
The sum of 5 and 3 is: 8
The sum of 10 and 7 is: 17

Static Methods

A static method belongs to the class rather than any specific instance. You can call a static method without creating an object of the class. Static methods are defined using the static keyword.

Example: A Static Method

Let's modify our Adder class to include a static method:

Adder.java
1public class Adder {
2 // Static method to add two integers
3 public static int addStatic(int a, int b) {
4 return a + b;
5 }
6
7 public static void main(String[] args) {
8 // Calling the static method without creating an object
9 int result = Adder.addStatic(5, 3);
10 System.out.println("The sum is: " + result);
11 }
12}
Output
The sum is: 8

In this example:

  • We define a static method addStatic that adds two integers.
  • In the main method, we call addStatic using the class name Adder.

When to Use Static Methods

  • Utility Functions: Methods that perform general utility tasks, such as mathematical calculations or string manipulations.
  • Factory Methods: Methods that create and return new instances of a class.

Public vs. Private Methods

Methods can have different access modifiers:

  • Public: The method is accessible from any other class.
  • Private: The method is only accessible within the same class.

Example: Public and Private Methods

Let's define both public and private methods in a class:

Calculator.java
1public class Calculator {
2 // Private method to perform multiplication
3 private int multiply(int a, int b) {
4 return a * b;
5 }
6
7 // Public method to perform addition
8 public int add(int a, int b) {
9 return a + b;
10 }
11
12 // Public method that uses the private method
13 public int calculateProductAndSum(int a, int b) {
14 int product = multiply(a, b);
15 int sum = add(a, b);
16 return product + sum;
17 }
18
19 public static void main(String[] args) {
20 Calculator calc = new Calculator();
21
22 int result1 = calc.add(5, 3);
23 System.out.println("The sum of 5 and 3 is: " + result1);
24
25 int result2 = calc.calculateProductAndSum(4, 6);
26 System.out.println("The product plus sum of 4 and 6 is: " + result2);
27 }
28}
Output
The sum of 5 and 3 is: 8
The product plus sum of 4 and 6 is: 32

In this example:

  • We define a private method multiply that can only be accessed within the Calculator class.
  • We define a public method add that can be accessed from outside the class.
  • We define another public method calculateProductAndSum that uses both the private and public methods.

Practical Example: A Bank Account Class

Let's create a practical example of a BankAccount class with various methods:

BankAccount.java
1public class BankAccount {
2 private String accountNumber;
3 private double balance;
4
5 // Constructor
6 public BankAccount(String accountNumber, double initialBalance) {
7 this.accountNumber = accountNumber;
8 this.balance = initialBalance;
9 }
10
11 // Public method to deposit money
12 public void deposit(double amount) {
13 if (amount > 0) {
14 balance += amount;
15 System.out.println("Deposited: $" + amount);
16 } else {
17 System.out.println("Invalid deposit amount.");
18 }
19 }
20
21 // Public method to withdraw money
22 public boolean withdraw(double amount) {
23 if (amount > 0 && amount <= balance) {
24 balance -= amount;
25 System.out.println("Withdrew: $" + amount);
26 return true;
27 } else {
28 System.out.println("Insufficient funds or invalid withdrawal amount.");
29 return false;
30 }
31 }
32
33 // Public method to get the account balance
34 public double getBalance() {
35 return balance;
36 }
37
38 // Static method to display a welcome message
39 public static void displayWelcomeMessage() {
40 System.out.println("Welcome to our bank!");
41 }
42
43 public static void main(String[] args) {
44 BankAccount.displayWelcomeMessage();
45
46 BankAccount account = new BankAccount("123456789", 1000.0);
47 System.out.println("Initial balance: $" + account.getBalance());
48
49 account.deposit(500.0);
50 System.out.println("Current balance: $" + account.getBalance());
51
52 account.withdraw(200.0);
53 System.out.println("Current balance: $" + account.getBalance());
54 }
55}
Output
Welcome to our bank!
Initial balance: $1000.0
Deposited: $500.0
Current balance: $1500.0
Withdrew: $200.0
Current balance: $1300.0

In this example:

  • We define a BankAccount class with private attributes for the account number and balance.
  • We provide public methods to deposit, withdraw, and get the balance.
  • We include a static method to display a welcome message.

Summary

ConceptDescription
MethodA block of code that performs a specific task.
Return TypeThe type of value returned by a method. Use void if the method doesn't return anything.
Parameter ListA list of parameters separated by commas. Each parameter has a type and a name.
Static MethodA method that belongs to the class rather than any specific instance.
Public MethodA method accessible from any other class.
Private MethodA method only accessible within the same class.

What's Next?

In the next topic, we'll explore how to create objects using constructors and initialize their attributes. Understanding constructors is essential for creating well-defined and functional Java classes.

Stay tuned for "Java Constructors"!


PreviousJava Class AttributesNext Java Constructors

Recommended Gear

Java Class AttributesJava Constructors