Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Applet question

 
kumar bangali
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Here's one on Applets. What happens when the following code is executed. The answer is 'runtime exception' since there is no 'no args' constructor. I could not figure this out. Could anyone explain this to me?
Thanx.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You have a constructor that has parameters. You do not have a no-arg constructor. If you have no constructors, a default no-arg constructor will be provided for you.

The Applet class that you are extending does not have a constructor matching the AppletTest constructor you have provided. Therefore you get an exception.
 
kumar bangali
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt. I was under the impression that the compiler will place a call to the 'no args' constructor of the super class implicitly using super() from inside the subclass constructor. Is this not the case?
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have to agree with you kumar.
i dont think the problem is that.
there will be a call of super() in your constructor.
 
kumar bangali
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think so too. But guess what.. I just tried to run this applet and it did end with a run time exception!! Beats me. Anyone who can throw light will be greatly appreciated.
Thanx
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
there is something weired going on with applets in different browsers. here is my view of things about this problem.
-insertion of default constructor is a 'runtime' issue dealt by the JVM running the class and creating objects. so, what happens is, there is no code inserted for default constructor in the class file generated by the compiler. i can be sure about this as i tried the following code,

here, AppletTest is assumed to be defined as in the problem statement.
now, when we run this code it gives only one constructor,
public AppletTest(java.lang.String)
this means it doesnt insert the default constructor code into the class file.
that should imply that, JVM does that for us whenever required at runtime.
now, if we come to the main problem then,
as JVM does invoke default const for us (still the class doesn't have default const code), it becomes the responsibility of IE or NN or the appletviewer program. and that is the reason its not working. as IE or NN or appletviewer doesnt do that. why?? i don know.
i observed following things running the following code in IE and NN.

on IE,
init method is called but instance initializer is not being called. that means it didn't invoke public default no-arg constructor. right? and so it didnt initialize display with the string "Hello world!" as well and i don't get any output on the applet. but still the applet works. it doesnt give me a runtime error. which i found strange thing as it didnt created object for the applet and still it didnt give exception of instantiation of the applet.
on NN,
i am getting runtime exception of instantiation of applet object.
so, i's sure that applet instance is not created in IE or NN. so, i made var display static and had a static initializer block for initialization of that var. it worked on IE. (it didnt work on NN as i's getting exception).
so, i conclude that IE and NN both dont insert that no-arg const to the applets and still IE behaves weiredly.
what i failed to understand is why the hell the code is not working properly for 'appletviewer' which might be using local JVM , which i'm sure support that no-arg const thing.
i made another conclusion. regardless of JVM, in applets it seem not the case that no-arg const is inserted if we dont have one and have a const with some argument.
of course, if we don have any const it works smoothly on IE, NN & appletviewer.
browsers are strange for applets. believe me. i worked for applets for sometime. u 'll go nuts resolving issues regarding applet display and loading in IE and NN browsers to make it work equally on both. i went mad dealing with that coz NN4.77, NN6.1, IE all behaves differently for an applet
so, thats about it. hope it helped little bit. still 'm not sure if any other strong and valid reason is there for this problem.
regards
maulin.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
by the way,
NN = Netscape Nevigator
IE = Internet Explorer
sorry for using short terms but they are easier that way.
regards
maulin.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info Maulin
just another reason why not to use applets (my opinion only guys ) .
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This problem has nothing to do with applets.
In the original code, you create an explicit constructor for your class with this signature:
public AppletTest (String s)
Because you have added an explicit constructor, the compiler will NOT add a no-arg constructor for you.
The ONLY time compiler adds a no-arg constructor is when you do not create ANY constructors for your class, period.
The runtime system is trying to instantiate your applet by calling the applet's constructor with this signature
public AppletTest()
Since there isn't one, you get a runtime error.
Rob
[ January 20, 2002: Message edited by: Rob Ross ]
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Rob, but isnt there a way to use another constructor than the default one when you create an applet???
 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Applets are loaded dynamically by JVM of browser. For any class which is being created dynamically you can not have constructor with parameter.
As here in case of applet ... browser has no way of knowing that which parameter to pass the constructor of YourApplet.
<param> tag in <applet> tag is to provide intial parameters to applet, which you can be accessed by method
String getParameter(String name)
of Applet class.
CMIW
HTH
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks kumar!
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh shit!
i forgot we have a const with arg in the applet.
so stupid of me! gotta keep cool before reading questions...as exam is approaching 'm getting little hasty..
-maulin.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic