• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exam Question

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.awt.*;
import java.applet.*;
class Test extends Applet
{
private String txt="hello";
Button b=new Button("ll");
public void init()
{
add(new Label(txt));
add(b);
}
// Test(int i) {}/Cnacel the constructor
//txt=string;} /label and button be shown
}
if have the constructor the label and button don't show
why I don't know.Who can help me? Thank you.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Hans,
I cannot understand what are you asking??The following code works fine!!!
What is the confusion ???
make your class public else it will give error applet not intialised......
the following code works......
<pre>
import java.awt.*;
import java.applet.*;
/*
the applet code tag goes here
*/
public class tes extends Applet
{
private String txt="hello";
Button b=new Button("ll");
public void init()
{
add(new Label(txt));
add(b);
}
}
</pre>
Harpal
[This message has been edited by Harpal Singh (edited November 06, 2000).]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hans,
I guess when you would have included the specified constructor, it would not have initialized the applet at all. It would have given an Start: Applet not initialised when you try to run the applet with an appletviewer. It would have thrown an InstantiationException which means that you cannot instantiate the applet through any other way than with the use of init() method which would be called by the appletviewer.
Moreover, an applet isn't guaranteed a full environment unless its init() method is called. Hence, inclusion of constructor would fail the applet's coming up. So, whatever initialisation is required, can be performed in the init() method of the applet itself.
Hope I am clear.
Aparna
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hans
Why are you including a constructor in a class extending the Applet class. You don't instantiate an applet extended class. Aparna stated perfect that when an applet execution starts the browser looks for the init() method first instead of any constructor.
You cannot have a constructor in an class extending the Applet. Am I right Aparna?
 
Aparna Narayanan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Tarit, it is an implementation constraint.
Thanks,
Aparna
reply
    Bookmark Topic Watch Topic
  • New Topic