Results 1 to 10 of 10
  1. #1
    oaklandsbest is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default Error in Code: Non-static method cannot be referenced from a static context

    Hey I am new to java and am in need of some help. Could you tell me why i am getting this error?

    "non-static method reverse(java.lang.String) cannot be referenced from a static context"


    Java Code:
    import java.util.Scanner;
    
    public class Reverse
    {
        public static void main(String args[]) 
        {
            Scanner keyboard = new Scanner(System.in);
            String input = keyboard.next();
            System.out.println(reverse(input));
        }
    
        public String reverse(String str)
        {
    
                if ((null == str) || (str.length()  <= 1)) {
                    return str;
                }
                return reverse(str.substring(1)) + str.charAt(0);
        }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Non-static methods belong to an instance of the class not to the class itself. If you want to call the method then you need to create an instance first.

  3. #3
    oaklandsbest is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    So if my file was called ReverseTest.Java I would add the line ReverseTest rev = new ReverseTest(); to my code. And then change println to System.out.println(rev.reverse(input));

    Is that what you are saying to do?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    (the file *has* to be called Reverse.java)

    You could try that and find out if it's effective. Or change reverse() to be static.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Did you try changing your code, compiling and running? It would have been much quicker than asking here.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    What happened when you tried it?

    db

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Quote Originally Posted by pbrockway2 View Post
    Or change reverse() to be static.
    Nooooooooooooooooooooo!

  8. #8
    oaklandsbest is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    I meant Reverse.java I was thinking of another file in the package I had. But I did change it, I was just asking because I saw that it worked, but wasn't sure if it was correct. I know in programming it might work but might also be the wrong approach to use.
    Last edited by oaklandsbest; 06-09-2011 at 08:28 AM.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Quote Originally Posted by Junky View Post
    Nooooooooooooooooooooo!
    Whyever not? It's a utility method that doesn't depend in any way on the state of the instance.

    Now if the Reverse class were to contain a String member set in the constructor, and the reverse() method didn't take a parameter but instead returned the reverse of that String, that would be a case for not making the method static.

    db

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Quote Originally Posted by DarrylBurke View Post
    Whyever not?
    The non-staitc/static error is seen a lot on fora and most often OP solves the problem by adding the static keyword. Ok, it might make sense in some cases but in others it doesn't. By offering that as a solution noobs will then take it as a green light to make everything static. This has 2 disadvantages: 1 they do not learn nor understand the difference and 2 they totally abandon the OO concept. Once someone gets into a bad habit it is hard to break.

Similar Threads

  1. Replies: 4
    Last Post: 04-16-2011, 05:15 PM
  2. Replies: 7
    Last Post: 05-19-2010, 11:12 AM
  3. 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
  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
  •