java - From static main method in same outer class like inner class, can't access to constructor from inner class -
why happened, gives me error in void main
when initializing newstring
, the method stringthread(string, int) undefined type mainthread
? here code:
public class mainthread { public class stringthread implements runnable { private int num; private string text; public stringthread(string text, int num){ this.text = text; this.num = num; } public void run(){ for(int = 0; < num;i++) system.out.println(i+1+". " + text); } } public static void main(string[] args){ stringthread newstring; newstring = stringthread("java", 30); new thread(newstring).start(); } }
new
keyword missing in initialization , that's why considering method , not constructor , it's inner class should be. (suggested stultuske
)
mainthread obj = new mainthread(); stringthread newstring = new obj.stringthread("java", 30);//new keyword missing
please read following answer understand why need access inner class,
Comments
Post a Comment