Why make private inner class member public in Java?

If the InnerEvenIterator class does not extend any class or implement any interface, I think it is nonsense because no other class can access any instance of it.

If the InnerEvenIterator class does not extend any class or implement any interface, I think it is nonsense because no other class can access any instance of it. However, if it extends or implements any other non private class or interface, it makes sense. An example: interface EvenIterator { public boolean hasNext(); } public class DataStructure { // ... private class InnerEvenIterator implements EvenIterator{ // ... public boolean hasNext() { // Why public?

// ... } } InnerEvenIterator iterator; public EvenIterator getIterator(){ return iterator; } }.

It is useful when you implement any interface. Class DataStructure implements Iterable { @Override public Iterator iterator() { return new InnerEvenIterator(); } // ... private class InnerEvenIterator implements Iterator { // ... public boolean hasNext() { // Why public? // ... return false; } @Override public DataStructure next() { throw new UnsupportedOperationException("Not supported yet."); } @Override public void remove() { throw new UnsupportedOperationException("Not supported yet.

"); } } public static void main(String ex) { DataStructure ds = new DataStructure(); Iterator ids = ds.iterator(); ids.hasNext(); // accessable } }.

This method can be made public in order to indicate that it's semantically public, despite the fact that compiler doesn't enforce visibility rules in this particular case. Imagine that during some refactoring you need to make this inner class top-level. If this method is private, how would you decide whether it should be made public, or some more restrictive modifier should be used?

Declaring method as public tells reader the intentions of original author - this method shouldn't be considered an implementation detail.

I think you are missing the implementing the Iterator interface part in your sample code. In that case, you can't make the hasNext() method have any other visibility identifier other than public since that would end up reducing its visibility (interface methods have public visibility) and it won't compile.

There are many combinations of access modifiers which are not useful. A public method in a private inner class is only useful if it implements a public method in a public class/interface. Public class DataStructure { // ... private class InnerEvenIterator implements Iterator { // ... public boolean hasNext() { // Why public?

// ... } } public Iterator iterator() { return new InnerEvenIterator(); } } BTW: abstract classes often have public constructors when actually they are protected.

1 It's not about being "useful"; you just don't have an option when it comes to implementing an interface. :-) – Sanjay T. Sharma Jun 7 '11 at 11:54 This is true but my point is; if you are not implementing or overriding a public method, the method doesn't need to be public and could have private scope for a private class.

Making it public is not useful if it cannot be accessed publicly and could be misleading or confusing. – Peter Lawrey Jun 7 '11 at 12: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