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

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.