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

  • Home
  • SEARCH
  • 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 6791269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:49:33+00:00 2026-05-26T17:49:33+00:00

In Java, a class can never be qualified to be either private or protected.

  • 0

In Java, a class can never be qualified to be either private or protected. It can simply be public all the times. It’s quite obvious, there is no question about it.


Inner classes in Java however, can be designated with any of these modifiers private, protected or public. An inner class can also be defined within an individual block like a method. In such a case, an inner class can not be designated with any of these modifiers, private, public or protected.


The following simple program is compiled with no errors and works just fine.

package amazingjava;
final class Demo
{
    public class Really
    {
        public class Quite
        {
            public class Surprising
            {
                @Override
                public String toString()
                {
                    return( "it really works !" );
                }
            }
        }
    }
}

final public class Main
{
    public static void main(String[] args)
    {
        System.out.println( "It may be hard to believe, but " + new Demo().new   
        Really().new Quite().new Surprising().toString());

        class Really
        {
            class Quite
            {
                class Surprising
                {
                    @Override
                    public String toString()
                    {
                        return( "it really works !" );
                    }
                }
            }
        }
        System.out.println( "It may be hard to believe, but " + new Really().new 
       Quite().new Surprising().toString());
    }
}

It produces the following output.

It may be hard to believe, but it really works !

It may be hard to believe, but it really works !


Now, my question is only that the inner classes declared inside the main() method in the above example can not have any modifiers. An attempt to qualify those classes with any of modifiers private, protected or even public issues a compile time error! Why? Which default modifier is used by the compiler in such cases?

  • 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-26T17:49:34+00:00Added an answer on May 26, 2026 at 5:49 pm

    A inner class defined inside a method is treated as any variable declared in that method – and those variables are LOCAL. A local variable or class cannot have any modifiers besides FINAL.

    The inner classes defined within another class are treated as instance variables, thus an instance class or a variable can be public private or have no identifier.

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

Sidebar

Related Questions

The RFC for a Java class is set of all methods that can be
How can I access a public static member of a Java class from ColdFusion?
How can hibernate can access a private field/method of a java class , for
like in java I have: Class.getSuperClass().getDeclaredFields() how I can know and set private field
The java.io.Console class requires there to be a Console device (window). How can I
Java .class files can be decompiled fairly easily. How can I protect my database
In java a class can implement Iterable which lets you use the foreach() statement
Will the fact that java class files can be decompiled and need of third
In Java, can Class.forName ever return null, or will it always throw a ClassNotFoundException
What is a synthetic class in Java? Why should it be used? How can

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.