Md. Nazrul Islam Md. Nazrul Islam - 1 month ago 8
Java Question

Why cant i call fu() from main method

public class NewClass {

static int x;
class Duck{
int y;
int fu(){
NewClass.x=45;
this.y=76;
return x+y;
}

}


public static void main(String[] args) {

System.out.println(new Duck().fu());
}
}

Answer

Your problem is that you're trying to create a Duck object but Duck is enclosed in NewClass so you must first create a NewClass object (Or make them both static)

try this

public static void main(String[] args) {
    Duck d = new NewClass().new Duck();
    System.out.println(d.fu());
}