Results 1 to 5 of 5
  1. #1
    ninjalord918 is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default Passign arguments

    Hello,

    I'm trying to learn how to create two classes, passing one argument to the other. But I'm getting an error, "Cannot make a static reference to the non-static field hl." What am I doing wrong?
    hello.java

    XML Code:
    import javax.swing.*;
    
    public class hello{
    	public static void main(String[] args)
    	{
    		String s = "hello world";
    		DisplayFunc df = new DisplayFunc(s);
    	}
    }
    DisplayFunc.java

    XML Code:
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    
    public class DisplayFunc {
    	hello hl;
    	public DisplayFunc(String s){
    		s = "-";
    	}
    	public static void main(String[] args)
    	{
    	   JFrame frame = new JFrame("Texts");
    	   JPanel pane = new JPanel();
               JTextArea jt = new JTextArea(hl.s);
    	   frame.setSize(400,400);
    
    	   frame.add(pane);
               pane.add(jt);
    	   frame.setVisible(true);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your code confuses me due to several issues:

    1: both classes have main methods which begs the question, which one do you want to have with a main method, and which one should be more of an OOPs class. My guess, hello should have a main and nothing but a main method, and DisplayFunc should not. In fact your may be partly stemming from DisplayFunc having a static method, which it shouldn't have. Quite possibly you meant to have a constructor in the place where you have DisplayFunc's main method.

    2: You appear to be referring to the hello class from within DisplayFunc and the DisplayFunc class from within hello. This can lead to big problems, and I don't think that you really want to do this.

    3: You have a hello variable hl that, and you appear to be trying to access a field of the class, s, that simply doesn't exist.

    4: the code just doesn't make sense. What are you trying to do? This is probably the biggest problem.

    My suggestions:

    1) First of all get rid of all this code, it's not worth correcting, and start over.
    2) Start out with a plan written out in English, not in Java.
    3) Then code from that plan.
    4) Then if you are still having problems, come back with your code and your plan/explanation of what you're trying to do.

    Luck.
    Last edited by Fubarable; 08-03-2010 at 10:32 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Edit: also you never replied to the last posts in your last thread: Total column issue again

    I think that most of us appreciate seeing some acknowledgment that you've read and understand what we've posted, or if you don't understand that you ask for clarification.
    Last edited by Fubarable; 08-03-2010 at 10:40 PM.

  4. #4
    ninjalord918 is offline Member
    Join Date
    Jul 2010
    Posts
    30
    Rep Power
    0

    Default

    Thank you for the advises, wrote down what I want, what each class does, and it works. This is what I've been trying to do all this time.
    hello.java
    XML Code:
    import javax.swing.*;
    
    public class hello{
    	public static void main(String[] args)
    	{
    		String s = "hello world";
    		DisplayFunc df = new DisplayFunc(s);
    	}
    }
    DisplayFunc
    XML Code:
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    
    public class DisplayFunc {
    	public DisplayFunc(String s){
    		JFrame frame = new JFrame("Texts");
    		JPanel pane = new JPanel();
    		JTextArea jt = new JTextArea(s);
    		frame.setSize(400,400);
    		frame.add(pane);
    		pane.add(jt);
    		frame.setVisible(true);		
    	}
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. passing arguments
    By mac in forum New To Java
    Replies: 3
    Last Post: 04-07-2010, 11:30 PM
  2. Replies: 6
    Last Post: 04-05-2010, 09:58 PM
  3. Arguments in Main
    By CyberFrog in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 09:37 PM
  4. repetition of 'arguments'(?)
    By Igor in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 10:08 AM
  5. Variable No. of Arguments
    By Gajesh Tripathi in forum New To Java
    Replies: 2
    Last Post: 10-31-2007, 02:50 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
  •