Why Class B can’t become public?
How can I use class be in other classes? Is it better to define it inside Cons?!
// public class B { why not?
class B {
int x;
B (int n) {
x=n;
System.out.println("constructor 'B (int n)' called!");
}
}
public class Cons {
public static void main(String[] args) {B b = new B();}
}
As per java language specification, there can be only one public class in a file (.java) and file name should be same as public class name.
If you want class B accessible in other placs, you may create a separate B.java file and move your Class B code to that file.
This thread may give you some more information.