문제

Let me explain further. I have a String function (called stringReversal) that returns a reversed string, it has no errors in the function. But, when I try to print using System.out.println() from the main function, it gives me the error "Can not make a static reference to the non static method stringReversal (string s) from the type StringReverse".

I tried giving my stringReversal a static modifier, but after doing so, it gave me run time errors.

Here's what my code looks like:

public class StringReverse {

    public String stringReversal(String s){

        if(s == null){
            return null;
        }

        else if(s.length()% 2 == 0){
            int size = s.length();

            for(int i =0; i<s.length(); i++){
                s.replace(s.charAt(i), s.charAt(size));
                size--;
                if(i == (s.length()/2) || size==0)
                    break;
            }
        }

        else{
            for(int i =0; i<s.length(); i++){
                int size = s.length();

                s.replace(s.charAt(i), s.charAt(size));
                size--;

                if(i == ((s.length()/2) +1) || size==0 )
                    break;
            }
        }

        return s;
    }

    public static void main(String[] args) {
        String str = "Hello";
        String rev = stringReversal(str);

        System.out.println();
    }

}
도움이 되었습니까?

해결책

You have to instantiate your class to call object members, or you need to make your function static, indicating it's not part of object oriented paradigm

In your case you can do

StringReverse sr = new StringReverse();
String rev = sr.stringReversal("hello");

or declare your method differently

public static String stringReversal(String s)

In fact the class name StringReverse itself does not sound like some kind of object, so the second way is preferred impo

The deeper problem you have is the confusion on how Java handle OO and entrance function in general. Java is primarily an OO language so most of the time everything shall be an object or a member of a object. But when you telling the VM to run some java code, there got to be a place to start, which is the main method. There has to be one main method and it must be under some class, but it really has nothing to do with the class that contains it. Within the main method, you either start your OO life by instantiating objects and invoking their members (method 1) or stay in the spaghetti world for a bit longer, by calling other static members as procedures (method 2).

다른 팁

You have two options:

  1. Keep the method non static and then create an instance of your class to call the method:

    public static void main(String[] args) {
        String str = "Hello";
    
        StringReverse sr = new StringReverse(); // instance of class
        String rev = sr.stringReversal(str);
    
        System.out.println(); // just prints a blank line lol...
    }
    
  2. Make the method static (you should do this):

    public static String stringReversal(String s) {
        // ...
    }
    
    public static void main(String[] args) {
        String str = "Hello";
        String rev = stringReversal(str);
    
        System.out.println(); // just prints a blank line lol...
    }
    

Either way, you have to fix your "run time errors". You can't get around that. If your method doesn't work, keeping it not static won't make it work either.

By the way, I think you meant to do System.out.println(rev); instead of System.out.println();


For the record, here is how to easily reverse a string (both methods work):

public static String stringReversal(String s) {

    StringBuffer reverseString = new StringBuffer();

    // reverse the string
    for (int i = s.length() - 1; i > -1; i--) {
        reverseString.append(s.charAt(i));
    }

    return reverseString.toString();
}

/* using the reverse() method in the StringBuffer class
   instead of reversing the string through iterations */
public static String stringReversal2(String s) {
    return new StringBuffer(s).reverse().toString();
}

This is happening because your Main method is static, but the class that it's in is not. In order to call a non-static method, you need to create an instance of the class. Alternatively, the method can be made static, but in order to refer to it you need to include the class name in your call (as if to use the class itself like an object containing the method - see below).

There are three solutions to this problem:

  • Make an instance of the class and call the method from your object (recommended).
  • make the method static and use StringReverse.stringReversal().
  • Make the class AND the method static.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top