public class Test { private Test(int n){ String strang = null; System.out.println("Test " + n + " " + strang); } protected Test(){ System.out.println("Protected"); Test n = new Test(1); } public static void main(String[]arg){ Test test = new Test(); } } 
  • @Lexis What's the problem? What confuses you? - Artem Konovalov
  • tidying up private Everything worked and works. - Alex Lexis
  • Artem Konovalov in flexibility. Why doesn't java beat me for it? - Alex Lexis
  • So you run it in the same class. And there you can get up with anything. Though to force pies to bake - Aleksey Shimansky
  • one
    @Lexis because in the constructor you create a new instance of the same class, and here the question is whether you need it or not, java cannot decide for you - Artem Konovalov

1 answer 1

you have public static void main(String[]arg){ is within sight of the methods and constructor with any modifier. so the code is executed.

If you put Main into another class, it will also work, because the protected modifier allows you to call a method from outside, but is not visible during inheritance.

  • access modifiers for the constructor work on the general principles as well as for the methods and for the class? - Alex Lexis
  • @Lexis yes you are right - Senior Pomidor