Error: non-static variable this cannot be referenced from a static context?
-
I am writing a Java GUI and I want to change the panel however when calling the GUI from the main function I get the error "error: non-static variable this cannot be referenced from a static context" when trying to make a new instance of my object on in case 1: import java.awt.*; import javax.swing.*; import java.util.*; public class addModify { //JPanels declared here JPanel airplanePane, airportPane, runwayPane, fuelPane; JFrame newFrame = new JFrame(); public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Hay"); String[] list = {"Airplane 1", "Airport 2", "Runway 3", "Fuel 4"}; for(int i = 0; i < 4; i++) { System.out.println(list[i]); } System.out.println("Please enter a menu option:"); int newInteger = input.nextInt(); switch(newInteger) { case 1: mainMenu test = new mainMenu("Hello", 500, 500); break; case 2: System.out.println("Ello2"); break; case 3: System.out.println("Ello3"); break; case 4: System.out.println("Ello4"); break; default: System.out.println("Default"); break; } } class mainMenu { public mainMenu(String title) { this(title, 500, 500); } //Constructor for title and width public mainMenu(String title, int w) { this(title, w, 500); } //Constructor for title, width, and height public mainMenu(String title, int w, int h) { newFrame.setTitle(title); newFrame.setSize(w,h); newFrame.setVisible(true); newFrame.setResizable(false); newFrame.add(airplanePane); } } class addAirplane { public addAirplane(String title) { airplanePane = new JPanel(); airplanePane.setLayout(new GridLayout(6,2)); JLabel newLabel; JTextField textArea; newLabel = new JLabel("Make"); airplanePane.add(newLabel); textArea = new JTextField(10); airplanePane.add(textArea); newLabel = new JLabel("Model"); airplanePane.add(newLabel); textArea = new JTextField(10); airplanePane.add(textArea); newLabel = new JLabel("Fuel Tank Size"); airplanePane.add(newLabel); textArea = new JTextField(10); airplanePane.add(textArea); newLabel = new JLabel("Fuel Burn Rate"); airplanePane.add(newLabel); textArea = new JTextField(10); airplanePane.add(textArea); newLabel = new JLabel("Fuel Type"); airplanePane.add(newLabel); textArea = new JTextField(10); airplanePane.add(textArea); newLabel = new JLabel("Airspeed"); airplanePane.add(newLabel); textArea = new JTextField(10); airplanePane.add(textArea); } } }
-
Answer:
I don't see where you're trying to create a new instance of your addModify class. Maybe you plan to do this later? For now, you'll get close to working if you declare the JPanels and the JFrame as static so they will be accessible from main(). You also need to make the nested classes mainMenu and addAirplane static too, as in: static class mainMenu ... static class addAirplane ... Otherwise, they are "inner classes" that have to be associated with an instance of the surrounding class...but there isn't any instance of addModify. By the way, these are odd classnames for Java. Conventionally, the class name begins with a capital letter (mixed case), while member names (except for the constructors) begin with a lowercase letter and capitalize the inner words ("camel" case...because of the "humps" in the middle.
Aaron at Yahoo! Answers Visit the source
Other answers
you are trying to access member functions of AirPlane etc classes in main of other classes. main is declared as static. we can call from main only static functions. However we can create objects in main with them we can call instance methods. We need to have an object to invoke member functions.
James Bond
Related Q & A:
- What is the difference between a static method and class method in Python?Best solution by pythoncentral.io
- How to access a non static method in an abstract class's static method?Best solution by Stack Overflow
- How do I add to an array from a static method?Best solution by stackoverflow.com
- Which of the following refers to a static muscle contraction?Best solution by Yahoo! Answers
- What's the difference between a static data member and a regular data member?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.