たとえば、 と の 2 つのクラスがClass1あり、 からフィールド (例: )を使用してステートメントClass2を作成したいとします。ifClass2int optionClass1
オブジェクトを作成してみました:
Class1 object = new Class1();
でClass2、次にifステートメントを記述します。
if(object.option == 2)
しかし、うまくいきませんでした。
import java.util.Scanner;
public class Class1 {
    public static void main(String[] args) {
        Class2 obj = new Class2();
        int option;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an option of 1, 2 or 3");
        option = input.nextInt();
        obj.input();
    }
}
public class Class2 {
    public void input(){
        Class1 object = new Class1();
        if(object.option == 1){
            System.out.print("You've selected the option 1");
        }
        else if(object.option == 2){
            System.out.print("You've selected the option 2");
        }
        else if (object.option == 3){
            System.out.println("You've selected the option 3");
        }
    }
}
コンパイル エラーが発生します:オプションをフィールドに解決できません