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.
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.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Joined: Dec 01, 2001
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?
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.
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
Joined: Jan 13, 2002
thanks Rob, but isnt there a way to use another constructor than the default one when you create an applet???
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
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Roy Ben Ami
Joined: Jan 13, 2002
Joined: Nov 04, 2001
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.