If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Is actionPerformed a static method ?

Hi, I'm new to Java and still trying to get a handle on all this object oriented stuff but I would appreciate it if one of you guys could explain this;

Suppose I create a class which is a panel with a combobox on it. I create two instances of this class and add them to the content pane. I also add a panel with a JButton on it to the content pane.

I want to select a number from each combobox so that when I press the JButton the actionPerformed method adds the two numbers I selected.

My problem is the actionPerformed method seems to be a static method (is this true ?) as a result JBuilder keeps telling me "non-static variable cannot be referenced from a static context". Does this mean I can never use clicking a button to perform a job that involves manipulating instance variables belonging to an object, surely this can't be true.

As I say I am new to object oriented programming and its quite possible that i have completely gone off on a tangent with this one but if anyone has any thoughts or suggestions I would really appreciate it.

Going back to your question about ActionPerformed - [even though it looks like destin has gotten to the meat of your immediate issue] ...

In Java, a static method is one defined by the "class definition" - the compiler uses that class definition and places one copy of the method in the "static" portion of the memory footprint of the application. No other copy of the method is put into memory - even if you instantiate the class. When a call is made to this method, it refers back to this single statement of code which is in the "static" or "unchanging" part of the memory footprint. That is why a static method is called by referring to the ClassName, not to a particular instance or object of the class.

This set-up would be fast but inflexible if ActionPerformed() was a static method. Any call to the method would be "universal" and deal only with the code available to the class outside of any method - rather than making use of the environment (methods, fields, etc.) encapsulated in a particular instance of the class. You don't want to have your application set up this way.

Thanks destin,
Your code works perfectly and has helped me a lot. The difference is that you created a new instance of the combo class and then referenced the panels from that instance whereas in my code firstPanel and secondPanel were static even though I wrongly thought I had made them two seperate instances of the PanelWithCombo class.

Hi nspils,
Thank you as well. It is important for me that I grasp the meaning of 'static', for me it is subtle. Your definition adds a lot to my understanding. You said "a static method is called by refering to the class name" and this is where I was going wrong.

Obiviously there is a lot here for me to think about but thanks to you guys I can begin to reasoon this one out.