SparkKiddo SparkKiddo - 1 year ago 57
Java Question

How to properly use a method of another class? (Basic Calculator) (Java)

New to programming and to this site. I have made a main class and a another class (which is a basic calculator). But when I try to call it from my main class gives me an error. Can anyone please give me a solution. I am coding in eclipse.

This is my main:

import java.util.Scanner;

public class MultiClass {

public static void main (String args[]){

Calculator.calculatorObject = new Calculator();



And this is my calculator class:

import java.util.Scanner;

public class Calculator {

public void Addition(){

Scanner takeValue = new Scanner (;

double num1, num2, answer;

System.out.println("Enter your first number: ");
num1 = takeValue.nextDouble();

System.out.println("Enter your second number: ");
num2 = takeValue.nextDouble();

answer = num1 + num2;

System.out.println("your answer is: " + answer);



Answer Source

You did not initialize the calculator object properly. You made it act like a parameter in the Calculator class instead of making it a parameter in your main class.

You should put this line:

Calculator calculatorObject = new Calculator();

Instead of this line:

Calculator.calculatorObject = new Calculator();

Initializing means that you want to give an object that you declared a value. Let's say that you declare an integer and call it i

int i;

But you want to initialize it too, because right now it has an empty value:

i = 89;

Or by intializing it right after declaring:

int i = 89;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download