How do I refer to non-static fields of objects created in the main method in other methods?

I decided to make my calculator code a little bit less chaotic and stored much of the code that used to be in the main method in other methods. It looks better, but here’s the problem: the code can’t be run. Whenever I refer to instance fields belonging to an object created in the main method in those new methods, my methods have trouble understanding what it is. Each method is called in the main method after I create my class objects so, I figured, by that point my methods would know them. Unfortunately, they don’t. I hoped moving all of my methods after the main method would help, but it didn’t

There’s one thing you should keep in mind. As you can see, some of my methods manipulate fields of more than one object. I can’t simply make a method like that non-static and call that non-static method on one object and then on the other (like I did with checkType()). Look at the decimalRejection() method, for instance. I have different responses depending on whether it’s the first number which is decimal, the second number, or both of them. So if both of them are decimal, I don’t want the program print, “Your first number is decimal! Your second number is decimal!” I want it to print, “Your both numbers are decimal”

How do I refer to non-static fields of objects created in the main method in other methods? Here’s my new code

import java.util.Scanner;
import java.text.DecimalFormat;

public class Num {
    private String stringValue;
    private char type;
    private double doubleValue;
    private static String operationS;
    private static boolean dontKnow;
    private static double result;
    private static final DecimalFormat df = new DecimalFormat("#.##");

    public Num(){

    }

    public static void main(String[] args) {
        Num firstNum = new Num();
        Num secondNum = new Num();

        Scanner scanner = new Scanner(System.in);
        firstNum.stringValue = scanner.next();
        operationS = scanner.next();
        secondNum.stringValue = scanner.next();

        firstNum.checkType();
        secondNum.checkType();

        if((firstNum.type || secondNum.type) == 'd') {
            decimalRejection();
        }

        if((firstNum.type || secondNum.type) == 'r') {
            romanHandling();
        }

        if((firstNum.type || secondNum.type) == 'i') {
            integerHandling();
        }

        calculatingResult();
    }

    public void checkType() {
        if (stringValue.compareTo(operationS) > 15) {
            type = 'r';
        } else {
            if ((int) (double) Double.valueOf(stringValue) == (double) Double.valueOf(stringValue)) {
                type = 'i';
            } else {
                type = 'd';
                dontKnow = true;
            }
        }
    }

    public static void decimalRejection(){
        if(firstNum.type == 'd' && secondNum.type != 'd'){
            System.out.print("It looks like your first number is decimal! ");
        } else if(firstNum.type != 'd') {
            System.out.print("It looks like your second number is decimal! ");
        } else {
            System.out.print("It looks like both of your numbers are decimal! ");
        }
        System.out.println("Can't work with decimals, sorry!");
    }

    public static void romanHandling() {
        firstNum.romanToArab();
        secondNum.romanToArab();
        if((firstNum.doubleValue || secondNum.doubleValue) == 0) {
            if(firstNum.doubleValue == 0 && secondNum.doubleValue != 0){
                System.out.println("I don't know the first number! It's supposed to be a Roman, isn't it?");
            } else if(firstNum.doubleValue != 0){
                System.out.println("I don't know the second number! It's supposed to be a Roman, isn't it?");
            } else {
                System.out.println("I know neither of the numbers! They are supposed to be Romans, aren't they?");
            }
        }
    }

    public void romanToArab(){
        switch(stringValue) {
            case "I":
                doubleValue = 1;
                break;
            case "II":
                doubleValue = 2;
                break;
            case "III":
                doubleValue = 3;
                break;
            case "IV":
                doubleValue = 4;
                break;
            case "V":
                doubleValue = 5;
                break;
            case "VI":
                doubleValue = 6;
                break;
            case "VII":
                doubleValue = 7;
                break;
            case "VIII":
                doubleValue = 8;
                break;
            case "IX":
                doubleValue = 9;
                break;
            case "X":
                doubleValue = 10;
                break;
            default:
                dontKnow = true;
        }
    }

    public static void integerHandling() {
        firstNum.doubleValue = Integer.decode(firstNum.stringValue);
        firstNum.doubleValue = Integer.decode(firstNum.stringValue);
        if((firstNum.doubleValue || secondNum.doubleValue) > 10){
            if(firstNum.doubleValue > 10 && secondNum.doubleValue != 10){
                System.out.println("It appears the first number is too big for me! I can only handle numbers of up to ten, remember!");
            } else if(firstNum.doubleValue != 10){
                System.out.println("It appears the second number is too big for me! I can only handle numbers of up to ten, remember!");
            } else {
                System.out.println("It appears both numbers are too big for me! I can only handle numbers of up to ten, remember!");
            }
            dontKnow = true;
        }
    }

    public static void calculatingResult(){
        if(operationS.equals("+")) {
            result = firstNum.doubleValue + secondNum.doubleValue; }
        else if(operationS.equals("-")) {
            result = firstNum - secondNum; }
        else if(operationS.equals("*")){
            result = firstNum * secondNum; }
        else if(operationS.equals("/")){
            result = firstNum / secondNum; }
        else {
            System.out.println("I don't know such an operation!");
            dontKnow = true; }
    }

    public static void calculatingResult() {
        if (!(operationS.equals("/") && secondNum.doubleValue == 0)) {
            if (!dontKnow) {
                if (result / (int) result != 1) {
                    if (String.valueOf(result).equals(df.format(result))) {
                        System.out.println("It's " + result + "!");
                    } else {
                        System.out.println("It's approximately " + df.format(result) + "!");
                    }
                } else {
                    System.out.println("It's " + (int) result + "!");
                }
            }
        } else {
            if (!dontKnow) {
                System.out.println("Gosh! I tried to divide it by zero, as you requested, but my virtual head nearly exploded! I need to recover...");
            } else {
                System.out.println("Besides, you can't even divide by zero, I'm so told!");
            }
        }
    }
}

I think you can’t do so directly.
You could pass the object as an argument to a method if you want to call the methods of that object or access its instance fields / properties.

1 Like

I did exactly that, thank you!