Results 1 to 4 of 4
  1. #1
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default non static method cannot be referenced from a static context

    Could someone please tell me why i'm getting this error ?
    non static VARIABLE cannot be referenced from a static context
    Thank you !

    Java Code:
    public class House{
     private String string1;
    
     public House(){}
     
     public String Method1{
     // do stuff
     return string1
    }
    public int Method2(String x){
    // do stuff
    }
    
    public static void main (String args[]){  
    
    Method1();
    Method2(string1);
    
    }
    }
    Last edited by Dex; 09-24-2012 at 06:37 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default Re: non static method cannot be referenced from a static context

    Suppose I want to say hello to you; in java that translates to me.sayHelloTo(you) where me and you are two objects of class Human. But that also implies that there has to be a 'me' and a 'you' and that is exactly your error: you want a House object to Method1 or Method2 but there is no House object (b.t.w. by convention method names start with a lower case letter).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: non static VARIABLE cannot be referenced from a static context

    Quote Originally Posted by JosAH View Post
    Suppose I want to say hello to you; in java that translates to me.sayHelloTo(you) where me and you are two objects of class Human. But that also implies that there has to be a 'me' and a 'you' and that is exactly your error: you want a House object to Method1 or Method2 but there is no House object (b.t.w. by convention method names start with a lower case letter).

    kind regards,

    Jos
    Hi there, sorry I made a mistake ( its 4 am) I did create an object of type "House" and used it as h.method1(string1) , but the error I was getting was a VARIABLE error not METHOD ( my mistake)

  4. #4
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: non static method cannot be referenced from a static context

    Solved...

Similar Threads

  1. Replies: 3
    Last Post: 05-31-2012, 07:56 PM
  2. Replies: 9
    Last Post: 06-10-2011, 12:40 AM
  3. Replies: 7
    Last Post: 05-19-2010, 11:12 AM
  4. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:05 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 AM

Posting Permissions

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