Question

Very newbie Q about JavaFX classes. Main is a main starting class in JavaFX, and it has no constructor (I dont know why)

Main.java

class Main extends Application {
   Log  log;   // class, not shown here
   Work work;
   @Override
   public void start() // entry point
   {
      log  = new Log("main.log"); // opens log file, need to be non-static
      work = new Work();
   }
}

Work.java

class Work{
     public Work() // constructor
    {
       Main.log.write("Making work object");  // error
       // What is the right method to call one non-static class from another ???
    }
Was it helpful?

Solution

try this

class Main extends Application {
   public static Log  log = new Log("main.log");   // class, not shown here
   Work work;
   public start() // entry point
   {
       work = new Work();
   }
}

then only you can use

Main.log.write("Making work object");

OTHER TIPS

Pass log to the Work instance.

class Main extends Application {
   Log  log;   // class, not shown here
   Work work;
   public start() // entry point
   {
      log  = new Log("main.log"); // opens log file, need to be non-static
      work = new Work(log);
   }
}

== Work.java ==

class Work{
    Log  log;
    public Work(Log log) // constructor
    {
       this.log = log;
       log.write("Making work object");           
    }

Your Log needs to be public and static.

Only when something is declared public can you access it in another class (without inheritance, I mean)

and

Only when something is declared static can you refer to it as Classname.VariableName

class Main extends Application {
   public static Log  log; // see the change ?   
   ...
   public void start() // this needed a return type
   {
      ...
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top