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 8621209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:46:09+00:00 2026-06-12T06:46:09+00:00

Possible Duplicate: Why does Java prohibit static fields in inner classes? I was going

  • 0

Possible Duplicate:
Why does Java prohibit static fields in inner classes?

I was going through the specification and got that it is not possible to have the static member in the inner class which is not final compile time constant .

class HasStatic {
    static int j = 100;
}
class myInnerClassTest {
    class Inner extends HasStatic {
        static final int x = 3;  // OK: compile-time constant
        static int y = 4;  // Compile-time error: an inner class
    }
    static class NestedButNotInner{
        static int z = 5;    // OK: not an inner class
    }
    interface NeverInner {}   // Interfaces are never inner
}

Whereas I got from the Why can we have static final members but cant have static method in an inner class? that it can inherit the static member from its owner class. But why it shouldn’t? What OOP’s principal it hurts?

  • 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-06-12T06:46:10+00:00Added an answer on June 12, 2026 at 6:46 am

    Your class myInnerClassTest isn’t declared as static. So what would that exactly mean for it to have a static field ?

    Would it be

    • the same for all instances whatever the enclosing instance ?
    • the same for all instances of this inner class having the same enclosing instance ?

    At first sight most programmers would probably think it’s the first case, while the encapsulation logic of the (non static) inner class should probably lead to the second choice. Either case (or both with different modifiers) would need a new definition of static which probably wasn’t seen as necessary. And in either case programmers would be confused about the exact meaning.

    From the specification :

    An inner class is a nested class that is not explicitly or implicitly
    declared static.

    Inner classes include local (§14.3), anonymous (§15.9.5) and
    non-static member classes (§8.5).

    Inner classes may not declare static initializers (§8.7) or member
    interfaces, or a compile-time error occurs.

    Inner classes may not declare static members, unless they are constant
    variables (§4.12.4), or a compile-time error occurs.

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

Sidebar

Related Questions

Possible Duplicate: Why does Java prohibit static fields in inner classes? Let's look at
Possible Duplicate: Why Java does not see that Integers are equal? I have 2
Possible Duplicate: Does Java guarantee that Object.getClass() == Object.getClass()? If I have a class
Possible Duplicate: Size-limited queue that holds last N elements in Java Does java include
Possible Duplicate: Java: Array of primitive data types does not autobox I've been doing
Possible Duplicate: Hints for java.lang.String.replace problem? Using string.replace() in Java Why / does not
Possible Duplicate: What does “(void) new” mean in C++? I'm not familiar with C++
Possible Duplicate: Why does (360 / 24) / 60 = 0 … in Java
Possible Duplicate: ^ operator in java I was assuming that c ^ d is
Possible Duplicate: Java - why no return type based method overloading? The compiler does

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.