Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1014089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:12:06+00:00 2026-05-16T10:12:06+00:00

I am not able to understand why this code doesn’t compile: class A {

  • 0

I am not able to understand why this code doesn’t compile:

class A {
    public static void main(String[] args) {
        System.out.println("hi");
    }
}

private class B {
    int a;
}

I am saving the contents in a file named A.java – and I get an error:

modifier private not allowed here // where I have defined class B

This happens both when I try B as private and protected. Can someone please explain me the reason behind this?

Thanks !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:12:07+00:00Added an answer on May 16, 2026 at 10:12 am

    From the Java Language specification:

    The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration

    So yes, the private and the protected modifiers are not allowed for top level class declarations.

    Top-level classes may be public or not, while private and protected are not allowed. If the class is declared public, then it can be referred to from any package. Otherwise it can only be referred to from the same package (namespace).

    A private top level class wouldn’t make much sense because it couldn’t be referred to from any class. It would be unusable by definition. private is OK for member classes to make a class referable to only it’s enclosing class.

    A protected member class can be referred to from (1) any class of the same package and from (2) any subclass of the enclosing class. Mapping this concept to top level classes is difficult. The first case is covered by top level class with no access modifiers. The second case is not applicable for top level classes, because there is no enclosing class or something else from a different package with a special relation to this class (like a subclass). Because of this I think, protected is not allowed because it’s underlying concept is not applicable for top level classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I cannot do it in form closing You have to… May 16, 2026 at 1:58 pm
  • Editorial Team
    Editorial Team added an answer As mentioned in your given link, this problem is in… May 16, 2026 at 1:58 pm
  • Editorial Team
    Editorial Team added an answer are you sure that your file has a size greater… May 16, 2026 at 1:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have this little bit of code: using System; using System.Web.Mvc; public class SecureFilter
I have been searching for a while and yet I am not able to
I'm not good at C# at all, I just don't get the logics. But
I have a dictionary that goes like this: typedef struct dictNode { int key;
This is a follow-on from: Debugging a Multithreaded C# - C++/CLI - C++ Solution
I'm trying to understand what is the correct why to implement COM interfaces from
I'm new to iPhone and Objective C programming but I'm able to make my
I know how to use properties and I understand that they implicitly call underlying
I am writing JVMTI code to profile Java programs, which mostly entails obtaining stack
In asking this question, I'm looking for either better understanding of the situation or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.