non-static method

Hello everyone ... I know this will be a pretty simple problem for some of you, but keep in mind that this is my first course in Java, so I am sorry if this question is dumb.

We have been studying methods, and are just now starting to write our own user-defined classes to create our objects from. I am working some of the problems in the back of the book, but I have run acorss a problem. I have two class files, Passenger, and AirPlane. They both compiled fine. I am working on my ProcessingApplication class, but I am stuck on an error.

ProcessingApplication.java:12: non-static method ControlPanel() cannot be referenced from a static context.

Here is my snippet of code pertaining to this error ...

public class ProcessingApplication {

public static void main( String args[] ){

//Main Control Panel

controlPanel();//call to control panel method

//close console

System.exit(0);

} //end method main

//method controlPanel - used to control navigation through program

//takes in no parameter, uses switch to determine what navigation to take ... returns nothing

public void controlPanel(){

//data members

String controlPanel = "";//control panel selection as string value

int navigate = 0;//parsed value for controlPanel

//prompt and parse

controlPanel = JOptionPane.showInputDialog( "Welcome! Please select your desired option ... \n 1) Enter flight customers \n 2) Run Cardinal Airlines Flight Reports \n 3) Exit system ");

navigate = Integer.parseInt( controlPanel );

//switch to control navigation

switch( navigate ){

case 1:

enterFlightInfo();

break;

case 2:

runFlightReport();

break;

case 3:

break;

}//end switch

}//end method controlPanel

Ofcourse enterFlightInfo and runFlightReport are going to be separate methods, all of which are prob. going to give me the same error.

I understand that private data members are hidden within that class file, and that static is a type of datamember that only has one copy which is accessible to all objects of that class. However, I have never run across this error I just posted. I tried putting keyword 'static' infront of void in my method declaration, but that didn't seem to work, plus that would just be rigging it to work.

If someone could just explain what the error means, and possible give me a little background information on the main method being static, that would be great. If I find an answer to my own question, I will reply to this post myself.

Thanks much

Rusty Rothwell

[2664 byte] By [Rusty] at [2007-9-30 22:30:01]
# 1

> I tried putting keyword 'static' infront of void in my method

> declaration, but that didn't seem to work, plus that

> would just be rigging it to work.

>

I copied and pasted your code, then changed the line to public static void controlPanel(){

and it compiled fine. Of course, like you said, this might not be the best solution. Typically, in the main method that is used to launch the application, you create instances of one or more classes, then use those instances to perform whatever it is you need to do.

In the ProcessingApplication class main method, you could useProcessingApplication app = new ProcessingApplication();

app.controlPanel();

Note that you use controlPanel as a method name and a variable name. This might lead to confusion and the wrong behavior of your code. Normally, methods perform some actions so method names have verbs in them. Your controlPanel method could be named selectFunction as an example. Also, the controlPanel String doesn't seem to fit. Perhaps using something as simple as selection for the String variable that is the result of the user's JOptionPane selection.

Variables declared inside methods should have names in lower case according to Sun Java naming conventions.

atmguy at 2007-7-7 12:53:21 > top of Java-index,Administration Tools,Sun Connection...
# 2
Thanks alot for the help, I appreciate it.- Rusty
Rusty at 2007-7-7 12:53:21 > top of Java-index,Administration Tools,Sun Connection...
# 3
You're welcom. Thanks for the Dukes.
atmguy at 2007-7-7 12:53:21 > top of Java-index,Administration Tools,Sun Connection...