Results 1 to 2 of 2

Thread: non-static variable jTxtHostName cannot be referenced from a static context

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    Question non-static variable jTxtHostName cannot be referenced from a static context

    Hello All,

    I am new to Java and I am learning it, I try to get the value from a function but .. it returns error:

    non-static variable jTxtHostName cannot be referenced from a static context

    jTxtHostName.setText(cls1.getComputerName());


    public String getComputerName()
    {
    String ret = "";
    try {
    ret = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException ex) {
    Logger.getLogger(Class1.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ret;
    }



    what I missed?

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: non-static variable jTxtHostName cannot be referenced from a static context

    You cannot call a non-static method from another static method.
    For example Class Test has 2 methods one is the public static void main and the other is "public String toString" You cannot call the method toString from the main function without instantiating a new "Test"
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

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



Click Here to Expand Forum to Full Width