0

How to make this code working?

This code comes from challenges, and I am not Java programmer so I ask somebody who is. I just want minor changes on this to make it working, thanks! public class Program { interface P{ void print(int x); } class Main implements P{ void print(int x){ System.out.print(x); } } public static void main(String[] args) { P ob = new Main(); ob.print(10); } }

11th Jan 2019, 2:21 PM
Dejan Dozet
Dejan Dozet - avatar
1 Answer
+ 2
1)make print() method inside Main class as public 2)use the reference of Program class to create the object of Main class because objects of non-static nested class can be created only with the reference of outer class public class Program { interface P{void print(int x);} class Main implements P { public void print(int x) { System.out.println(x); } } public static void main(String args[]) { Program ref=new Program(); Main ob = ref.new Main(); ob.print(10); } }
11th Jan 2019, 2:58 PM
Rishi Anand
Rishi Anand - avatar