Code compiles, runs as applet but not as application

Code compiles, runs as applet but not as application

I am very much a beginner at Java and have very little programming knowledge.

I have a major project I wish to do but have started learning with simple small programs.

I have a problem with a simple practice application that I have discovered does not occur if I try the program as an applet. What I have tried to do is set up a frame with a number of buttons and a single label. I tried to display the button label in the label field when the button was clicked.

If my code is of the form:

import java.awt.*;import java.applet.*;public class Demo1 extends Applet {Label lab;public void init () {	code to set up frame, buttons and label  }public boolean action (Event e, Object o) {code to setText of label to label of button }return true;}}
then this compiles OK and works.

However, what I want to do is develop an application, but when I replace the init() method with the main() method and try to compile, I get loads of errors related to the reference to ‘lab’ in my action method, errors such as “cannot reference member … without an object.”

I also get errors about the setLayout method .

What am I doing wrong?

It’s hardto tell from looking at your code what the problem is with setLayout. One problem I see is that lab is a variable member of instances of yourDemo1 class, while main() is required to be a method member of the Demo1class/object itself, so it can’t reference public instance members withoutqualifying which instance they belong to. (eg. f.lab, not lab).

Unlike C++, Java is purely an object-oriented language. This means everythingin Java is an object (except for ints, chars, booleans, floats, …).Even classes are objects!

When you start the Java application Demo1.class,the Java class loader first loads the Demo1 class object. Keep in mind that no instances of the Demo1 class have yet been created, so no members ofDemo1 instances (like lab), can yet be referenced. Next, Java invokesthe main() method inside Demo1. Fortunately, because the main() method wasdeclared static, it is a member of the Demo1 class/object, so it can bereferenced at this time.

Now main() takes over. Here is where instances of the Demo1 class wouldnormally be created and initialized:

class Demo1 extends Frame {         Label lab;      public boolean action(…) { … }      public static void main() {         Demo1 f = new Demo1(); // create Demo1 instance         f.setLayout(new FlowLayout());         f.setBackground(Color.white);         f.add(new Button(…);         f.lab = new Label(“hi”);         f.add(f.lab);         // etc.      }   }
Notice that because lab is a member of the Demo1 instance frather than the Demo1 class/object, it must be qualified insidemain() as f.lab. The same is true for setLayout(), add, etc.

A more conventional way to convert applets into applications isto convert the init() method into a constructor for the applicationrather than the main() method, which isn’t even an instance member.

To do this, simply replace “public void init()” with “public Demo1()”,then add a simple main() method:

public static void main() {      Demo1 f = new Demo1(); // this is your constructor      f.resize(200, 300);;   }


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist