Java Puzzle: modifiers?

There is no value in a non-public class having a public constructor since, as you rightly state, the class isn't accessible outside of that scope. It will compile - that's just the way things are - but many code analysis tools will generate a warning.

There is no value in a non-public class having a public constructor since, as you rightly state, the class isn't accessible outside of that scope. It will compile - that's just the way things are - but many code analysis tools will generate a warning. There is value in a non-public class having public methods if the class extends or implements a public class or interface, since the derived class can stand in for the base class or interface type: package mypackage; class MyRunnable implements Runnable { private final String message; MyRunnable(String message) { this.

Message = message; } @Override public void run() { System.out. Println(this. Message); } } public class Surprises { public static Runnable getSurprise() { return new MyRunnable("boo!"); } } Code outside of mypackage can then obtain a MyRunnable instance (as a Runnable) and call the public method: Runnable r = Surprises.getSurprise(); r.run(); I'll ignore your second question since it was answered elsewhere in the comments.

That's what I needed! Thanks a lot! – Dmitry Nov 13 '10 at 21:47.

Public modifiers are allowed on non-public classes because public methods defined in those classes may need to be overridden in public subclasses. E.g. Abstract class MyBaseClass { public abstract void method(); } public class MySubClass extends MyBaseClass { @Override public void method() { ... } } Rather than impose a restriction on constructors that does not appear on other members, public constructors are allowed on classes regardless of the visibility of the containing classes.

5 But you could always give an overridden method greater visibility than the corresponding method in the superclass. In your example above, you could have left the public keyword off of MyBaseClass.method() and everything would still compile fine. – Asaph Nov 13 '10 at 20:05 That post raises the question again!

– Dmitry Nov 13 '10 at 20:46.

It may be useful for inheritance. Class B, B. B1,b2 belongs to class A, they are inside A.

– Dmitry Nov 13 '10 at 20:50 class B is inside class A. Why B's private members shouldn't be visible for class A? – khachik Nov 13 '10 at 20:59 1.

) package access would also do for inheritance - if the class has package access, you have to be in the same package to extend it. – Pete Kirkham Nov 13 '10 at 21:10 (I was going to suggest a public constructor makes a difference to reflection, but it doesn't) – Pete Kirkham Nov 13 '10 at 21:13.

It's much more convenient then it could be. Just imagine that you have a public class, and then you understand that you can make it package-private, and after that compiler tells you that there's 45 errors (on every of your public methods). Cannot reply on your second question.

I also think that it's weird.

Downvoter: care to leave a comment. – Roman Nov 13 '10 at 20:00 2 -1 I read this answer 4 times and it still doesn't make sense to me. – Asaph Nov 13 '10 at 20:01 2 So did I.

Please, give more detailed answer – Dmitry Nov 13 '10 at 20:49 1 If you have 45 different constructors I'd say you have other problems. – Pete Kirkham Nov 13 '10 at 21:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions