+ 10
Can you declare a class as private ?
If no then describe the reason
7 ответов
+ 10
You can make a class private only if it is nested inside another class. In that case the private class can only be accessed by the methods of the outer class, but not from outside.
https://www.tutorialspoint.com/java/java_innerclasses
+ 7
Denise Roßberg why are you saying it's bad idea to use make it as private
+ 5
I would say a private class would be useless.
private class Test{
//some code
}
How you should you create an object of this class? And how to call a.method from it. Nothing would be possible.
public class Test{
private class Hidden{
//some code
}
}
This works because the class test has access. So you can work with the hidden class.
+ 4
bad Idea if it is not nested, because you have not any access then
+ 3
Charan LEo25
The whole class would be unreachable code. That's why it is not allowed to make a class private.
+ 2
A private class has no meaning. We would not be able to create objects outside, and won't be able to call methods.