• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why the subclass of javafx.application.Application needs to be public?

 
Quazi Irfan
Ranch Hand
Posts: 85
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a bear bone code of a javafx app. I removes all the other parts for the sake of simplicity.

The code only works with fxclass access specifier set to public. If I remove public specifier, the code throws the following error,


My question is why I need to set the fxclass access specifier to public to make the code work?

I found this correction by accident. I didn't have a clue what this error meant. Can anyone point me out where in the error is says I need to set the class public.
 
John Damien Smith
Ranch Hand
Posts: 296
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> why I need to set the fxclass access specifier to public to make the code work?

The java launcher that executes your application is not in the same java package as your application, so unless you assign the public access rights for your application, the launcher cannot find your application because the application is invisible to it - if the launcher can't find the application, the application cannot be run.

> Can anyone point me out where in the error is says I need to set the class public.

It doesn't explicitly say that, but that is not uncommon for error messages - often they will give you an indication of what went wrong rather than telling you exactly what is wrong. In this case the indication is the NoSuchMethodException on a reflective application constructor call. It is telling you that the Java launcher is looking for a constructor for the class and cannot find it. As the public/private/package/protected keywords in Java are information hiding constructs for modifying visibility scope, I think it would naturally lead you to check the visibility scope you have given your application.

  • When you call launch() on a JavaFX application, some strange things happen due to the quirky way the launching code is implemented using reflection.
  • If you are super-curious, you can study the code snippets from the launcher in the answer to this question: http://stackoverflow.com/questions/28113690/javafx-only-allow-1-class-to-call-a-method-from-a-singleton-class
    To better understand reflection (which internally the launcher is using to launch your application) you could study the Java reflection trail.
  •  
    Campbell Ritchie
    Sheriff
    Posts: 51349
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Doesn't that mean it also has to have a public no‑arguments constructor, too? (Or if not public, accessible via reflection.)
     
    John Damien Smith
    Ranch Hand
    Posts: 296
    14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    > Doesn't that mean it also has to have a public no‚ÄĎarguments constructor, too? (Or if not public, accessible via reflection.)

    Exactly Campbell.

    The following simple hello world program will generate an exception unless you comment out the private constructor to allow the default public no-arg constructor to be used when launching the application instance.
    The exception generated would be "Caused by: java.lang.NoSuchMethodException: PrivateHelloWorldApp.<init>()".

     
    Campbell Ritchie
    Sheriff
    Posts: 51349
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you . (And sorry for delay in replying)
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic