Page 1 of 1

non-static method

Posted: Sun Feb 18, 2018 12:18 am
by maty.tsoraro@gmail.com
this is the URL link of the code
http://www.beta.browxy.com#USER_157350

getting an error
non-static method g(java.lang.String,int,int) cannot be referenced from a static context

Re: non-static method

Posted: Mon Feb 19, 2018 1:24 am
by dbremmen@gmail.com
Hi,
The error means that you cannot invoke a non static method from a static context.
In your case when you have a static method, you cannot call a method from a class that is not static. To be able to execute your program you need to create a new instance of the object Test.
In your code I added the new Test() before calling the method g

Code: Select all

public class Test{
    public static void main(String[] args){
       new Test().g("hello world",0,1);
    }
public void g (String str,int i ,int j){
    if(i<str.length()){
        g(str,i+1,-1*j);
        if(j==1)
        System.out.print(str.charAt(i));
    
    }
}
}

Re: non-static method

Posted: Tue Feb 20, 2018 4:44 am
by maty.tsoraro@gmail.com
got it ,thanks !