Results 1 to 5 of 5
  1. #1
    wizmang is offline Member
    Join Date
    Apr 2008
    Posts
    1
    Rep Power
    0

    Default Non-Static method in static context error

    Hi, I have a section of code from my program which uses a method inherited from another class.

    private void processLabyMenu( KeyEvent event ) {
    int keycode = event.getKeyCode();
    String x = new String();

    if( Prax.Cfg.getString("returnToIntro").equalsIgnoreCa se
    (KeyEvent.getKeyText(keycode)) )
    {
    mainWindow.setMapFile( null );
    mainWindow.load();
    }


    and I get the error
    "the non-static method getString(java.lang.String) cannot be referenced from a static context".

    what can I put in that "if" statement for it to compile?

    Thank you in advance...

  2. #2
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Hei,
    Can you post the compleate code?

  3. #3
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    The message is clear: you are calling a a non static method (Prax.Cfg.getString) from a static context. It seems Prax.Cfg is a inner class and you should use an instance of that class in the if condition.
    But that being said, a bigger chunk of the code will help with understanding.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  4. #4
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    Add the keyword 'static' to getString.

    eg:
    Java Code:
    public static String getString()
    {
    }
    Did this post help you? Please me! :cool:

  5. #5
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Quote Originally Posted by danielstoner View Post
    The message is clear: you are calling a a non static method (Prax.Cfg.getString) from a static context. It seems Prax.Cfg is a inner class and you should use an instance of that class in the if condition.
    But that being said, a bigger chunk of the code will help with understanding.
    This is correct you need to create an instance of the class your trying to access. For example

    Java Code:
    public class Demo
    {
          Prax myPrax;
    
           public Demo()
           {
                 myPrax = new Prax();
                 myPrax.Cfg.getString();
           }
    }
    Think of it this way the:

    The way your doing it is calling the actual class. That could have multiple instances of it running in your system. This is fine if your class is static meaning it doesn't change when you create multiple instances of it.

    What you want to do is edit a new instance of it.

    When you work on your car in real life, are you working on the model car that the manufacture made or are you working on your "instance" of that car.
    Last edited by Zosden; 04-24-2008 at 08:55 AM.

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2009, 12:35 AM
  2. Replies: 0
    Last Post: 04-16-2008, 11:07 PM
  3. is overriding static method possible
    By raghu in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 12:38 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09: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
  •