Hi, I am a newbie in java. I am preparing for SCJP2 right now. this is a mock question during my study. I did not understand what is wrong with the code. please throw some light in. 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. Select the most appropriate answer. the answer given is B. I need a clear explanation, plz. [Spaces added inside HTML tags to make them visible to browsers. - Jim] [This message has been edited by Jim Yingst (edited July 06, 2000).]
Tony Alicea
Desperado
Sheriff
Joined: Jan 30, 2000
Posts: 3219
posted
0
Applets should not have constructors and all initialization activity for them should be in the <CODE>init()</CODE> method. In this example, the JDK can't instantiate the class Sample. Take it out as below and the applet works as expected. Finally, good advice if you're serious in passing the SCPJ2 Certification: Get a Java compiler and practice, practice, practice This code works: <PRE>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; } */ }</PRE>
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.