0
0
Javaprogramming~15 mins

Static methods in Java - Mini Project: Build & Apply

Choose your learning style8 modes available
folder_codeUsing Static Methods in Java
📖 Scenario: You are creating a simple calculator program that can add and multiply numbers. To keep things organized, you want to use static methods inside a class so you can call them without creating an object.
🎯 Goal: Build a Java class called Calculator with two static methods: add and multiply. Then call these methods from the main method to show the results.
📋 What You'll Learn
Create a class named Calculator
Add a static method add that takes two int parameters and returns their sum
Add a static method multiply that takes two int parameters and returns their product
In the main method, call Calculator.add and Calculator.multiply with the numbers 5 and 7
Print the results of both method calls
💡 Why This Matters
🌍 Real World
Static methods are used for utility functions like math calculations, formatting, or helper methods that don't need object data.
💼 Career
Understanding static methods is important for writing clean, reusable code and is commonly used in Java programming jobs.
Progress0 / 4 steps
1
Create the Calculator class
Create a public class called Calculator with an empty body.
Java
💡 Need a hint?

Use the class keyword followed by the class name Calculator.

2
Add static methods add and multiply
Inside the Calculator class, add two public static methods: add and multiply. Both take two int parameters named a and b. add returns the sum of a and b. multiply returns the product of a and b.
Java
💡 Need a hint?

Static methods use the static keyword and can be called without creating an object.

3
Add the main method to call static methods
Add a public static void main(String[] args) method inside the Calculator class. Inside it, call Calculator.add(5, 7) and store the result in an int variable named sum. Also call Calculator.multiply(5, 7) and store the result in an int variable named product.
Java
💡 Need a hint?

The main method is the program's entry point. Use the class name to call static methods.

4
Print the results
Inside the main method, add two System.out.println statements to print the values of sum and product. The first line should print exactly Sum: 12 and the second line should print exactly Product: 35.
Java
💡 Need a hint?

Use System.out.println to print text and variables. Use + to join strings and variables.