Results 1 to 6 of 6
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default static vs. non-static

    ok, so this is driving me nuts! lol

    i have a java file called tasks.java. i call on a method in userGUI.java called setUp. in tasks, it tells me this: "Cannot make a static reference to the non-static method setUp() from the type userGUI". ok, so i change the method in gui to static. then, variables i have declared right after the class name in gui, now need to be static, my setUp method is saying, when i try to initialize them. what is going on here? i think it all starts with the main method, right, which is static? so then everything that follows from there needs to be static as well. am i right in this thinking?

    thanks.

  2. #2
    arielb is offline Member
    Join Date
    Jan 2012
    Location
    Panamá
    Posts
    41
    Rep Power
    0

    Default Re: static vs. non-static

    You must make an instance of the class to use a static method.
    PHP Code:
     userGUI gui = new userGUI();
    gui.setUp();

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: static vs. non-static

    Don't respond to a "Cannot make a static reference to the non-static method setUp() from the type userGUI" message by making things static.

    Usually only the few line main() method that launches your application will be static, and possibly the odd "static final" variable that is acting as a constant.

    There's not really enough in your post to see what the problem is. If you can't correct the code post a small example that shows the compiler's message when an attempt is made to compile it.

  4. #4
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: static vs. non-static

    Quote Originally Posted by arielb View Post
    You must make an instance of the class to use a static method.
    PHP Code:
     userGUI gui = new userGUI();
    gui.setUp();
    Do You have to create instance of class, when using static method?
    Or You create instance of class when using non static method?

  5. #5
    arielb is offline Member
    Join Date
    Jan 2012
    Location
    Panamá
    Posts
    41
    Rep Power
    0

    Default Re: static vs. non-static

    the error says:

    Cannot make a static reference to the non-static method setUp() from the type userGUI
    PHP Code:
    public static void m1(){
          //is not allowed.
          m2();
    }
    public void m2(){
         //If allowed.
         m1();
    }

  6. #6
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: static vs. non-static

    alright. along the same lines, i am having an issue with this:

    i declare textArea in gui.java:

    Java Code:
    JTextArea textArea;
    i start up the gui end...
    Java Code:
    public void startGUI() {
    			// These are all essential GUI pieces
    			JLabel jLabInstruction, jLaberror;
    			JLabel copyright = new JLabel("");
    			JTextField uI = new JTextField("");
    			JTextArea textArea = new JTextArea("");
    			JButton jbtnSubmit;
    			
    			final JFrame jfrm = new JFrame("app name!");
    			jfrm.setLayout(new FlowLayout());
    			jfrm.setSize(300, 300);
    			jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			textArea = new JTextArea(5, 20);
    			textArea.setEditable(false);
    			textArea.setLineWrap(true);
    			textArea.setWrapStyleWord(true);
    			jLabInstruction = new JLabel("SYSTEM: Please type in a command: ");
    			jbtnSubmit = new JButton("Submit");
    			jLaberror = new JLabel("");
    			textArea.setMargin(new Insets(10,10,10,10)); 
    					
    			jfrm.add(jLaberror);
    			jfrm.add(textArea);
    			jfrm.add(jLabInstruction);
    			jfrm.add(uI);
    			jfrm.add(jbtnSubmit);
    			jfrm.add(new JSeparator(SwingConstants.HORIZONTAL));
    			jfrm.add(copyright);
    			jfrm.setVisible(true);
    			// printToTextArea("hello");
    		}
    and i have a method that writes to the textArea above:
    Java Code:
    public void writeToTextArea(String userInputText) {
    			textArea.append("\nSYSTEM: "
    					+ userInputText);
    		}
    also, in tasks.java, i am able to call on this last method:
    Java Code:
    gui.writeToTextArea("PROGRAM STARTED!");
    my problem is that the text area field is not updating. nothing is being inputted.

    any and all help is appreciated!

    and thanks for the fast previous responses! they were helpful!

Similar Threads

  1. Replies: 9
    Last Post: 06-10-2011, 01:40 AM
  2. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  3. Replies: 4
    Last Post: 05-12-2010, 12:05 PM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •