Search code examples
javaclassprivateprogram-entry-point

Call Private Class from Main. Both in Public Class


My code looks like this:

public class Hello{

    private class Word{ 
        ... 
    }

    public static void main(String[] args) {

        Word W = new Hello.Word();
    }
}

How can I call the class Word from main?

Word W = new Hello.Word(); 

seems not to be the right solution.


Solution

  • You are trying to instantiate non-static class inside another, from a static context.

    Either make the inner class static:

     private static class Word{ 
        ... 
     }
    

    (However, you wouldn't need the Hello part, just Word W = new Word(); would do perfectly)

    Or create an instance of the outer class, and then create an instance of the inner class using that. (Sotirios suggested this solution too, but with better details.)