Home > Software engineering >  How do I print out the instance variable x from my Main class using a 'Second' class? [clo
How do I print out the instance variable x from my Main class using a 'Second' class? [clo

Time:09-17

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

CodePudding user response:

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

  • Related