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

19 / 65 topics
19Java Methods20Java Method Parameters21Java Method Overloading22Java Scope23Java Recursion
Tutorials/Java Programming/Java Methods
☕Java Programming

Java Methods

Updated 2026-04-20
5 min read
mdx
1export const meta = { title: 'Java Methods', description: 'Learn how to create and call methods in Java, including the differences between static and public methods.', lastUpdated: '2026-05-12', readTime: '30 min read', order: 19 };

Java Methods

In this tutorial, you'll learn about methods in Java, which are blocks of code that perform specific tasks. Understanding how to create and call methods is fundamental to writing organized and reusable code. This knowledge will help you break down complex problems into manageable parts and make your programs more modular.

Introduction

Methods are essential components of any programming language as they allow you to encapsulate functionality, promote code reusability, and improve readability. In Java, methods can be defined within classes and can be called by creating an instance of the class or directly if they are static.

In this tutorial, we will cover:

  • How to create and call methods in Java.
  • The difference between static and public methods.
  • Best practices for using methods effectively.

Creating Methods

A method in Java is defined with a return type, a name, and parameters (if any). Here's the basic syntax:

Java
1returnType methodName(parameters) {
2 // Method body
3}

Example: A Simple Method

Let's start by creating a simple method that prints "Hello, World!" to the console.

HelloWorldMethod.java
1public class HelloWorldMethod {
2
3 // Define a method named printMessage
4 public void printMessage() {
5 System.out.println("Hello, World!");
6 }
7
8 public static void main(String[] args) {
9 // Create an instance of HelloWorldMethod
10 HelloWorldMethod hello = new HelloWorldMethod();
11
12 // Call the printMessage method
13 hello.printMessage();
14 }
15}
Output
Hello, World!

Explanation

  • Return Type: void indicates that this method does not return any value.
  • Method Name: printMessage is the name of the method.
  • Parameters: None in this case.
  • Method Body: The code inside the curly braces is executed when the method is called.

Calling Methods

To call a method, you need to create an instance of the class (if it's not static) and then use the dot notation followed by the method name.

Static vs Public Methods

Java provides different types of methods based on their access modifiers. Two common types are static and public.

Static Methods

A static method belongs to the class rather than any object of the class. You can call a static method without creating an instance of the class.

Example: Static Method

Let's create a static method that prints "Hello, World!" using the main method as a static context.

StaticMethodExample.java
1public class StaticMethodExample {
2
3 // Define a static method named printMessage
4 public static void printMessage() {
5 System.out.println("Hello, World!");
6 }
7
8 public static void main(String[] args) {
9 // Call the static printMessage method
10 StaticMethodExample.printMessage();
11 }
12}
Output
Hello, World!

Explanation

  • Static Keyword: The static keyword indicates that this method belongs to the class rather than an instance.
  • Calling Static Method: You can call a static method using the class name followed by the dot notation.

Public Methods

A public method is accessible from any other class as long as it has the necessary access permissions. Public methods require an instance of the class to be called.

Example: Public Method

Let's create a public method that prints "Hello, World!" and call it using an instance of the class.

PublicMethodExample.java
1public class PublicMethodExample {
2
3 // Define a public method named printMessage
4 public void printMessage() {
5 System.out.println("Hello, World!");
6 }
7
8 public static void main(String[] args) {
9 // Create an instance of PublicMethodExample
10 PublicMethodExample example = new PublicMethodExample();
11
12 // Call the public printMessage method
13 example.printMessage();
14 }
15}
Output
Hello, World!

Explanation

  • Public Keyword: The public keyword indicates that this method is accessible from any other class.
  • Calling Public Method: You need to create an instance of the class and use the dot notation to call the public method.

Best Practices for Using Methods

  1. Keep Methods Short and Focused: Each method should perform a single, well-defined task. This makes your code easier to understand and maintain.
  2. Use Descriptive Method Names: Choose meaningful names that clearly describe what the method does. This improves code readability.
  3. Avoid Overloading Methods with Too Many Parameters: If a method has too many parameters, consider breaking it down into smaller methods or using objects to encapsulate related data.

Practical Example

Let's create a practical example that demonstrates the use of both static and public methods in a real-world scenario. We'll create a simple calculator class that performs basic arithmetic operations.

Calculator.java
1public class Calculator {
2
3 // Static method to add two numbers
4 public static int add(int a, int b) {
5 return a + b;
6 }
7
8 // Public method to multiply two numbers
9 public int multiply(int a, int b) {
10 return a * b;
11 }
12
13 public static void main(String[] args) {
14 // Call the static add method
15 int sum = Calculator.add(5, 3);
16 System.out.println("Sum: " + sum);
17
18 // Create an instance of Calculator
19 Calculator calc = new Calculator();
20
21 // Call the public multiply method
22 int product = calc.multiply(4, 2);
23 System.out.println("Product: " + product);
24 }
25}
Output
Sum: 8
Product: 8

Explanation

  • Static Method: The add method is static and can be called using the class name.
  • Public Method: The multiply method is public and requires an instance of the Calculator class to be called.

Summary

  • Methods in Java encapsulate functionality and promote code reusability.
  • Static Methods belong to the class and can be called without creating an instance.
  • Public Methods require an instance of the class to be called.
  • Best practices include keeping methods short, using descriptive names, and avoiding overloading with too many parameters.

What's Next?

In the next tutorial, we will explore how to pass parameters to methods and understand method parameter types. This knowledge will further enhance your ability to write flexible and powerful Java programs.


PreviousJava ArraysNext Java Method Parameters

Recommended Gear

Java ArraysJava Method Parameters