Code compiles, runs as applet but not as application

Code compiles, runs as applet but not as application

Question:
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?

Answer:
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);      f.show();   }

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes