The Java book I'm using (Java Programming by Joyce Farrel, Thompson Course Technology), when explaining how to add components to an applet, uses the following code:

import javax.swing.*;
import java.awt.*;
public class JHello extends JApplet
     Container con = getContentPane();
     JLabel greeting = new JLabel("Hello. Who are you?");
     public void init()

However, it doesn't explain how a Container object can be created without using the keyword new and its constructor. I've looked in three books, several websites, and asked someone for an answer, but I haven't gotten one so far. It's really not impeding my progress through the book; I'm just curious about this.

JApplet is a "top-level" widget, and getContentPane() returns the JPanel (which is a Container) to which items could be added.

Do you understand the "extends" keyword, and, if so, did you bother to read the API docs for that getContentPane() method, which is the one returning the container?

My knowledge of Java only extends as far as the book I'm using has explained, and the only thing it has said of the "extends" keyword so far is that the new class inherits the traits of the JApplet class. It explains inheritance in more detail two chapters later, but I haven't read that far yet (although I did skim that section for answers). I did look at the API doc for the JApplet and Container classes, though. I just didn't understand it well. I'm supposing that I need to learn more about inheritance to fully grasp this?


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.