• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple question on Applet

 
Suvojit Chakraborty
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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • 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: 42967
73
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic