Can we declare class as private?what will happen if we declare like that?

Showing Answers 1 - 16 of 16 Answers


  • Feb 25th, 2006


      a class can not be private if we declare like that the error will be modifier private not allowed here. So the class can't be declared as private.

  Was this answer useful?  Yes


  • Mar 6th, 2006


We can declare class as private. Only top level class can't be private.

public class PrivateClassTest {

private class PrivateClass{



  • Mar 13th, 2006

we can declare java class as private . But the tat class wont b accesslible from its outer scope like

public class B{

// member variables and methods

private class C{

// member variables and methods



now the prob wid dis is only the members of class B can access dis class C

and by OOP design, we want more reusability of code which cant b possble in dis case

  Was this answer useful?  Yes


  • Mar 15th, 2006

A class cannot be declared private

  Was this answer useful?  Yes

vikas sahu

  • Apr 6th, 2006

Hi all

We can use private,protected,static,default only with inner class not with the outer class.

  Was this answer useful?  Yes

Bimales Mandal

  • Apr 19th, 2006

If a class is declared as private then no one can access the class.

  Was this answer useful?  Yes


  • Aug 25th, 2006

Not true, the class can be accessed by an outer-class.

  Was this answer useful?  Yes

ch madhav

  • Jul 28th, 2011

yes.But we can not declare the outer class as private..If did so,the compiler will generate the error as the modifier private is not allowed here,but we can declare the inner class as private.

  Was this answer useful?  Yes


  • Jul 28th, 2014

Some people might get confused by the many posts. The correct answer to the question "can we declare class as private?" is YES, BUT only as an inner class. A normal class cannot be protected or private.

  Was this answer useful?  Yes


  • Jan 23rd, 2015

it will use in same class in same packages and use different class in different packages.

  Was this answer useful?  Yes

Sunil Chausali

  • Feb 24th, 2015

Yes, We can declare the class as a private but only inner classes can be a private.In that case we cant be use these class outside the class.

  Was this answer useful?  Yes

Thomas John

  • Sep 22nd, 2015

If you have a private class on its own as a top-level class, then you cant get access to it from anywhere. Inner class can be made private because the outer class can access inner class where as it is not the case with if you make outer class private.

  Was this answer useful?  Yes


  • Dec 6th, 2015

Child class will not be able to inherit it.

  Was this answer useful?  Yes


  • Jul 8th, 2017

java class cant be private it throws compile time exception.
Illegal modifier for the class TestClass; only public, abstract & final are permitted

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions