• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic Applet Doubt

 
Satish Kota
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is that an applet constructor should be declared always public?

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor is called by the JVM when is the applet is, well, constructed. It couldn't do that if the constructor wasn't public.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
The constructor is called by the JVM when is the applet is, well, constructed. It couldn't do that if the constructor wasn't public.


Ulf Dittmer, if I am not wrong, this is true ONLY for JavaBeans and Applet. You can have a plain java class without constructor and JVM can create object of it.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because if the class does not have any constructor, the compiler will insert a public no-argument constructor for you. It doesn't do that if there is constructor, so if the one that's there isn't public, the class can't be instantiated by a non-subclass in a different package.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!