Take the 2-minute tour ×

First I would like to say that I have only worked with java for 1 month now. This is probly a pretty simple question. I searched and the classic fruit example did not make any sense to me. I spent hours looking at it and trying to figure out how to apply this to make it work. It does not make any sense to me because everyone explains how to get and set a property with 2 lines of code and no structure statements. I would really appreciate a breakdown in how to talk to non-static from static.

I would like to setText in text box in my OBD2nerForm class from a separate and static class.

public class OBD2nerForm extends java.awt.Frame {

/** Creates new form OBD2nerForm */
public OBD2nerForm() {

public String setText(String text){

I think I have a static reference to this instance of the form defined here..

public class Status {
    public static OBD2nerForm form = new OBD2nerForm();

it is called from my main like this

public class obd2ner {

Then when I try to call it.. Status.form.getText gives me the initial values when the form is created. When I setText, it does not change the one on the screen.

I am just displaying this to keep it simple. There are many other parts going on. I have a static monitor on a serial port and I want it to grab the next data to be sent from the text box and then increment it.

I just don't understand how to use a getter and a setter on a non-static. It's not quite doing what I need it to do. It seems like I am seeing one instance on my screen and it is using a new instance to perform the getting and setting.

I tried this as per an answer I received, but it did not work...

public class OBD2nerForm extends java.awt.Frame {
      String X = "";

//Trying out the runnable method of incrementing the code public String getNewScannerValueRunnable(){

    Runnable doWorkRunnable = new Runnable() {

        public void run() { 


    return X;

I could really use some other suggestions. i just don't understand what has to happen here.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Your form is being created fine, and there's just one reference to it, and it's ending up in that static variable. All is well up to that point.

There's a 'secret' of Swing you need to be aware of: You cannot (visibly) change the properties of GUI objects from any thread other than the Swing thread, aka the Event Dispatching Thread.

The trick to doing it anyway is to pass the property-changing code as a Runnable to either of SwingUtilities.invokeAndWait() or SwingUtilities.invokeLater().


OK, let's back up. Your form is AWT based, not Swing based, so I'm afraid my advice on using SwingUtilities would probably not have helped you, even if you had implemented it correctly. I'll try to give more specific hints this time.

You've created a class OBD2nerForm that's an AWT form. That class has a constructor which calls initComponents to set up some GUI components on the screen.

The class also has a method called setText that will put its argument text into one of the fields on the form. That method is an instance method, i.e. it's not "static", as you'd call it.

You have another class, Status with a class field form. The initializer for form calls the constructor for OBD2nerForm. That will create an instance of the form and store it in form; but I haven't seen a show() or setVisible() call being made to the form to actually display it.

Here are the first signs of trouble:

 public class obd2ner {

Class names (like obd2ner) should start with capital letters; but that's a matter of style and convention, it's not what's causing you problems. Following the conventions helps other people read and debug your code, though.

The bigger problem is obd2ner.main() calling your form's main(). That could be made to work, but it's usually a sign that you're doing something wrong.

While nothing stops you from coding static main methods into as many of your classes as you want, only one of those main's can be started from the outside, i.e. when you run your application. The first main is essentially the 'boss' method for your program.

The "first main" usually instantiates and initializes a few objects. In a non-GUI application, main() may then start up a loop or some other control structure, wherein it will then orchestrate the actions of the other objects. In a GUI application, main() will usually just instantiate and then show the GUI, and then end; once the GUI is visible, all further program activity is triggered by actions the user performs on the GUI.

Without seeing your code, I'm guessing that Obd2nerForm.main() also instantiates Obd2nerForm, and shows it. So you probably indeed have one instantiated but invisible form hanging off Status.form and another one instantiated, visible and referenced from some variable in Obd2nerForm. If you want to influence that GUI, you need to make a reference to that form accessible.

Probably the simplest would be:

In Obd2nerForm, declare a public static Obd2nerForm form, and in Obd2nerForm.main, right after you call the constructor, copy the reference to the form into that variable. From then on, you can access the form and its methods using e.g. Obd2nerForm.form.setText().

A reminder, though: You seem to have two main()s, and this needs fixing. All the stuff that should be done at the beginning of the app's lifetime needs to be in one of those mains, not several.

Finally, look at this method call:


That's the syntax for calling a method on a particular instance. But Obd2nerForm.main is a class method (what you call "static"), and so there isn't "a particular one" to call, it's always just the one that belongs to the class itself. That's why the syntax to call a class method is something like


The compiler lets you get away with the way you wrote it, but it's not how it's usually done, and indicates some confusion.

There... I hope that gets you a little further along. If you still have problems, please post a more complete code sample to PasteBin and I'll take a look!

share|improve this answer
I tried this but it did not work... public String getNewScannerValueRunnable(){ Runnable doWorkRunnable = new Runnable() { @Override public void run() { Status.form.getNewRotatingValue();} }; SwingUtilities.invokeAndWait(doWorkRunnable); return X; } String X = ""; Should I just make all of my formattedTextBoxes static? –  Adam Outler Aug 1 '10 at 2:04
No, making everything static will only complicate your life. I've edited my answer to point out some of your problems, and hopefully that will help you straighten out your program. Alternatively, I'd strongly recommend learning GUI programming in better depth by closely following a tutorial or two - your main problem, as you're probably aware, is lack of general Java programming knowledge. But that's normal for just 1 month. –  Carl Smotricz Aug 1 '10 at 11:43
Thank you. You helped me out... Apparently I was referencing form.main() which creates a new form. All I needed to do was form.setvisible. I was looking at a new form and an invisible form was running in the background. –  Adam Outler Aug 2 '10 at 13:12
Thanks for your feedback! I put a fair bit of work into my analysis and I'm gratified that it has proven helpful. Happy hacking! –  Carl Smotricz Aug 2 '10 at 15:40
Everything you said makes sense. I will be going back and taking al of your advice. –  Adam Outler Aug 5 '10 at 20:40
add comment

Your Answer


