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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:01:05+00:00 2026-05-20T01:01:05+00:00

First I wanted to ask Why does Java prohibit static fields in inner classes?

  • 0

First I wanted to ask Why does Java prohibit static fields in inner classes?, but the question is already there. The reason I gave for needing them (serialVersionUID) was eliminated by the answer by Bozho. However, I’m still curious:

Are static fields prohibited only in the source code or also in the class file?

Btw., a convincing reason why this is prohibited is yet to be invented. In C there are static variables allowed even inside of functions. Their lifespan is the same as of any other static variable, just their visibility is different. The same would work in Java, too.

  • 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-20T01:01:06+00:00Added an answer on May 20, 2026 at 1:01 am

    Nothing really bad would happen if they were design to allow static stuff. There is no problem at class file level, I think, the byte code has no concept of inner class.

    It would be confusing though:

    class X
        class Y
            static int z;
    
    X x1 = new X();
    X.Y y1 = x1.new Y();    
    
    X x2 = new X();
    X.Y y2 = x2.new Y();    
    X.Y y3 = x2.new Y();    
    

    Intuitively, y2 and y3 should share the same z, but should it be the same z seen by y1?


    Conceptually an inner class is only valid within the outer instance. It’s even imaginable that JVM unloads it when the outer instance is GC’ed; the argument given in [1] against class unloading doesn’t apply, because inner classes have no static variable or static initializer.

    Yet the reality is, there is one class shared by all inner instances, regardless of outer instances. This is certainly true: y1.getClass()==y2.getClass()==y3.getClass().

    It can be argued that the language spec dictates that it be true: [2]

    Two reference types are the same run-time type if … defined by the same class loader, and have the same binary name

    y1’s class and y2’s class have the same class loader, the the binary name of X.Y is well defined and independent of outer instance. So y1 and y2 must have the same runtime class.

    If the language spec actually implied that an inner class is independent of the outer instance, the generally accepted explanation against static state in inner classes is weakened.


    [1] http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.7

    [2] http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.4

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

Sidebar

Related Questions

First of all, I know how to build a Java application. But I have
I wanted to edit just the first line of a 4 MB file. When
This is my first post here and I wanted to get some input from
First off, there's a bit of background to this issue available on my blog:
First let me say that I really feel directionless on this question. I am
First, let's get the security considerations out of the way. I'm using simple authentication
First off, I am using Windows XP. I have multiple hard drives and it
First off, I understand the reasons why an interface or abstract class (in the
First off if you're unaware, samba or smb == Windows file sharing, \\computer\share etc.
First off: I'm using a rather obscure implementation of javascript embedded as a scripting

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.