Is it possible to declare class as private?

1 answer

Answer

1237918

2026-04-19 07:05

+ Follow

Yes, but not in a normal way.

Sample code:

namespace WikiAnswers {

Java
Java

public class MainClass {

protected class HereItIs {}

}

}

The inner class MainClass.HereItIs is the answer to this question.

If you change the accessibility of MainClass from public to protected, the compilation will fail.

My interpretation of the keyWord protected is: "accessible to the class itself and its derived classes", and may only decorate/mark it to class elements (data members, methods, properties, etc.)

Noramlly, we declare a class as a namespace element, e.g. MainClass above, I almost answered 'NO' to this question when I read the question the first time. Tricky.

The above example may be applicable to Java:

1. replace namespace to package

2. the rest of the codes should be the same

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.