Search code examples
javac++typesprivatepublic-method

Private parameters in public methods


Neither g++ nor javac emit warnings when the parameters to non-private methods are of private types (e.g., private nested classes). Such methods cannot be used by clients, but they can appear as part of a class's public API.

In C++, putting such methods into the public section of a class Foo, for example, could allow other classes to access these methods without explicitly being listed as friends inside class Foo (so long as they can access the private types used as parameters).

But in general, would it be bad style to not explicitly make such methods private, or can this just be ignored? (since clients can't use it anyway, what's the big deal)


Solution

  • This is allowed and might be required.

    While you might not be allowed to create a variable of the private type; it might be return by a function. This would allow data to be transfered around the system without being persistent in parts of the system that have no reason to hold a reference to it.

    This idiom is not common but is allowed.