Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 139
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 387
30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
  •  
    Marshal
    Posts: 75638
    354
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Rancher
    Posts: 387
    30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 75638
    354
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you . (And sorry for delay in replying)
     
    If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
    Free, earth friendly heat - from the CodeRanch trailboss
    https://www.kickstarter.com/projects/paulwheaton/free-heat
    reply
      Bookmark Topic Watch Topic
    • New Topic