Results 1 to 6 of 6
  1. #1
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Question error message - non-static

    I hope someone can explain an error message that I routinely seem to get:

    "non-static method varOut() cannot be referenced from a static context Stub.varOut()"

    Here's the context. I have one class's method call another class's (Stub) method (varOut).

    I have a work around. In the first class, I create a Stub object:

    private Stub st = new Stub();

    then I call the method with:

    st.varOut();

    It then works fine, but I would like to know why.

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    306
    Rep Power
    6

    Default

    Take a look here for some help understanding the difference between instances and static methods/variables...

    Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    If you really want to use Java, I suggest getting a good introductory book. I personally the Core Java books (two volumes) by Sun Press.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Quote Originally Posted by newbie123 View Post

    I have a work around. In the first class, I create a Stub object:

    private Stub st = new Stub();

    then I call the method with:

    st.varOut();

    It then works fine, but I would like to know why.
    To be more specific, this is not a workaround, but the exact way methods are supposed to work in Java. Instance methods work on specific objects, static methods work independently from a instance of their class.

  5. #5
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    So as people have said, this is how they are supposed to work. What you have to do is figure out if you want your method to be static or not.

  6. #6
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Smile Many thanks.

    Thanks to all those who posted a reply to my question regarding "static" error messages. They were helpful in understanding the nature of the problem.

Similar Threads

  1. Replies: 1
    Last Post: 02-10-2009, 10:03 AM
  2. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 08:51 AM
  3. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  4. Replies: 1
    Last Post: 08-01-2007, 09:25 PM
  5. error message on jsp
    By sandor in forum Web Frameworks
    Replies: 1
    Last Post: 04-11-2007, 02:10 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
  •