This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDCert Q - Applet

 
vidhya Ramachandran
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the given code produce runtime exception w.r.t the constructor ?
-----------
import java.applet.Applet;
import java.awt.*;
public class Sample extends Applet {
private String text = "Hello World";
public void init() {
add(new Label(text));
}
public Sample (String string) {
text = string;
}

}
 
Surya Bahadur
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Provide a default constructor for the class or remove the constructor which takes the string,it works.
Surya
 
Dev Prakash
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Applets are invoked by Browsers's JVM and they expect a public no-arg constructor. If you do not specify a constructor, compiler automatically adds one no-arg constructor BUT when you define your own constructor, compiler does not put any, hence the error.
-Dev Prakash
 
Vikram Misra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Applets does not have constructors instead they have init()
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic