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 };
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.
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:
A method in Java is defined with a return type, a name, and parameters (if any). Here's the basic syntax:
1returnType methodName(parameters) {2// Method body3}
Let's start by creating a simple method that prints "Hello, World!" to the console.
1public class HelloWorldMethod {23// Define a method named printMessage4public void printMessage() {5System.out.println("Hello, World!");6}78public static void main(String[] args) {9// Create an instance of HelloWorldMethod10HelloWorldMethod hello = new HelloWorldMethod();1112// Call the printMessage method13hello.printMessage();14}15}
Hello, World!
void indicates that this method does not return any value.printMessage is the name of the method.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.
Java provides different types of methods based on their access modifiers. Two common types are static and public.
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.
Let's create a static method that prints "Hello, World!" using the main method as a static context.
1public class StaticMethodExample {23// Define a static method named printMessage4public static void printMessage() {5System.out.println("Hello, World!");6}78public static void main(String[] args) {9// Call the static printMessage method10StaticMethodExample.printMessage();11}12}
Hello, World!
static keyword indicates that this method belongs to the class rather than an instance.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.
Let's create a public method that prints "Hello, World!" and call it using an instance of the class.
1public class PublicMethodExample {23// Define a public method named printMessage4public void printMessage() {5System.out.println("Hello, World!");6}78public static void main(String[] args) {9// Create an instance of PublicMethodExample10PublicMethodExample example = new PublicMethodExample();1112// Call the public printMessage method13example.printMessage();14}15}
Hello, World!
public keyword indicates that this method is accessible from any other class.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.
1public class Calculator {23// Static method to add two numbers4public static int add(int a, int b) {5return a + b;6}78// Public method to multiply two numbers9public int multiply(int a, int b) {10return a * b;11}1213public static void main(String[] args) {14// Call the static add method15int sum = Calculator.add(5, 3);16System.out.println("Sum: " + sum);1718// Create an instance of Calculator19Calculator calc = new Calculator();2021// Call the public multiply method22int product = calc.multiply(4, 2);23System.out.println("Product: " + product);24}25}
Sum: 8 Product: 8
add method is static and can be called using the class name.multiply method is public and requires an instance of the Calculator class to be called.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.