• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Applet Help!

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Would you help me? Why the "Hello World" will not show when I open my browser to test the following code? Does exam will test Applet too? Thanks!
Q. 64
The following code defines a simple applet:

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;
}
}

It is accessed form the following HTML page:

<html>
<title>Sample Applet</title>
<body>
<applet code="Sample.class" width=200 height=200></applet>
</body>
</html>

What is the result of compiling and running this applet:
A. Prints "Hello World".
B. Generates a runtime error.
C. Does nothing.
D. Generates a compile time error.
Answer is B

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes!!! Correct Answer is B, It will create runtime error as browser JVM does not find any public no-arg constructor for the applet.
It do you do not specifically put any constructor, compiler will put one public no-arg constructor. BUT you provide your own compiler will not put any.
In this example, you have provided the constructor like this;
public Sample (String string) {
text = string;
}
So compiler did not put any and at runtime it failed as browser's JVM did'nt find any public no-arg constructor for the Applet.
I guess, you have the point clear now.
- Dev Prakash
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
First of all you must remember:
"If you define even a single constructor, the default constructor is not provided"
therefore when you try to run the program then it can't find the default constructor which is
public Sample(){
}
adding this constructor will throw no exception.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'mchen'
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Ajith
 
reply
    Bookmark Topic Watch Topic
  • New Topic