Skip to content
Advertisement

How do I print out the instance variable x from my Main class using a ‘Second’ class? [closed]

package com.company;
public class Main {
    int x = 5;
    }

//below is a separate class I have created to run my code in:

package com.company;

import java.sql.SQLOutput;

class Second {
    public static void main(String[] args) {
        Main myObj = new Main();
        System.out.println(myObj.x);
    }

}

This is the error message I get when I run my second class

Error: Main method not found in class com.company.Main, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

Advertisement

Answer

I have just found out – I was running my class Second and I needed to run class Main as that is where my main() method is located! Cheers anyway

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement