Error
Cannot use this in a static context.
Error Type
Compile Time
Sample Code
class BuggyBread2{
public static void test(){
System.out.println(this.method());
}
public String method(){
System.out.println("String Method");
return null;
}
}
public static void test(){
System.out.println(this.method());
}
public String method(){
System.out.println("String Method");
return null;
}
}
Cause
this is used in java to refer to current object. Static methods can be accessed using Class name too wherein it won't make any sense of this.
Resolution
Remove this from the static method
class BuggyBread2{
public static void test(){
System.out.println("");
}
public String method(){
System.out.println("String Method");
return null;
}
}
or Make the method non static.
class BuggyBread2{
public void test(){
System.out.println(this.method());
}
public String method(){
System.out.println("String Method");
return null;
}
}