Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Simple question on Applet

 
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it neccessary to make an applet subclass as public ?
I am asking this because I have this following simple applet code :



It executes normally.

However when I remove the public modifier from the class the applet window does open but display is not performed, status bar shows message Applet not initialized and exception is thrown on closing the window.

Following exception is thrown:

D:\Education\Java\JavaStudyRoom\Applets>appletviewer xyz.html
load: appExample is not public or has no public constructor.
java.lang.IllegalAccessException: Class sun.applet.AppletPanel can not access a
member of class appExample with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
at java.lang.Class.newInstance0(Class.java:349)
at java.lang.Class.newInstance(Class.java:308)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:619)




Thanks for clearing my doubt.
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is necessary.
 
Suvojit Chakraborty
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Yes, it is necessary.



Thank you for replying.
But why should it be public. In my knowledge there is no rule which says so. In fact I guess the rule is we can have a file with no public class but if we have a public class then the name of the file must match the name of the public class and in that file we cant have any more public classes.

 
Ulf Dittmer
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In my knowledge there is no rule which says so.


There is, in fact, such a rule that applies to this case.

Classes must be public to be accessible outside of their package or outside of subclasses. And since the classes that implement the applet environment (which instantiate the applet class) are outside of the applet's package, the applet class must be public.
 
Suvojit Chakraborty
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:

In my knowledge there is no rule which says so.


There is, in fact, such a rule that applies to this case.

Classes must be public to be accessible outside of their package or outside of subclasses. And since the classes that implement the applet environment (which instantiate the applet class) are outside of the applet's package, the applet class must be public.



Thanks for sharing the knowledge
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic