Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam

 
beginner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone explain this for me? Thanks in advance.
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;
}
}
The HTML page:
< html>
< title>SampleApplet< /title>
< body>
< applet code ="Sample.class" width=200 height=100>< /applet>
< /body>
< /html>

Answer is "Generates a running error"
[This message has been edited by Jim Yingst (edited July 28, 2000).]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The convention with browsers as containers of applets is:
The browser creates the applet object by calling the no-args constructor. When you provide a constructor with an argument, the compiler will NOT automatically generate a no-args constructor.
Therefore the browser attempt to create a Sample object fails.
Bill
 
beginner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill but after I remove the Sample constructor I still
got run time error. Can you run the piece of code and see what's happen? thanks,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic