Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
May 13, 1997

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 hard to 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 your Demo1 class, while main() is required to be a method member of the Demo1 class/object itself, so it can't reference public instance members without qualifying which instance they belong to. (eg. f.lab, not lab).

Unlike C++, Java is purely an object-oriented language. This means everything in 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 of Demo1 instances (like lab), can yet be referenced. Next, Java invokes the main() method inside Demo1. Fortunately, because the main() method was declared static, it is a member of the Demo1 class/object, so it can be referenced at this time.

Now main() takes over. Here is where instances of the Demo1 class would normally 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 f rather than the Demo1 class/object, it must be qualified inside main() as f.lab. The same is true for setLayout(), add, etc.

A more conventional way to convert applets into applications is to convert the init() method into a constructor for the application rather 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();
   }
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date