This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Applets and the fly likes Simple question on Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Simple question on Applet" Watch "Simple question on Applet" New topic
Author

Simple question on Applet

Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 62
    
    1
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
Marshal

Joined: Mar 22, 2005
Posts: 41054
    
  43
Yes, it is necessary.


Ping & DNS - my free Android networking tools app
Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 62
    
    1
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
Marshal

Joined: Mar 22, 2005
Posts: 41054
    
  43
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

Joined: Nov 10, 2009
Posts: 62
    
    1
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
 
wood burning stoves
 
subject: Simple question on Applet
 
Similar Threads
Actionperform not working
adding Frame to Applet
Problem with getCodeBase()
JDBC Problem.... Why no Answer
appletviewer