+ 10

Can you declare a class as private ?

If no then describe the reason

29th Jul 2019, 6:17 PM
Harshita Verma
Harshita Verma - avatar
7 Réponses
+ 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
29th Jul 2019, 7:27 PM
Tibor Santa
Tibor Santa - avatar
+ 7
Denise Roßberg why are you saying it's bad idea to use make it as private
30th Jul 2019, 1:38 PM
Charan Leo25
Charan Leo25 - avatar
+ 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.
29th Jul 2019, 8:57 PM
Denise Roßberg
Denise Roßberg - avatar
+ 4
bad Idea if it is not nested, because you have not any access then
30th Jul 2019, 1:46 PM
zemiak
+ 3
Charan LEo25 The whole class would be unreachable code. That's why it is not allowed to make a class private.
31st Jul 2019, 6:41 AM
Denise Roßberg
Denise Roßberg - avatar
+ 2
A private class has no meaning. We would not be able to create objects outside, and won't be able to call methods.
1st Aug 2019, 5:19 AM
Chiradeep Debnath
Chiradeep Debnath - avatar