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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:20:40+00:00 2026-06-01T11:20:40+00:00

I have a couple of questions relating to nested classes within Java. How do

  • 0

I have a couple of questions relating to nested classes within Java.

  • How do nested classes appear “under the hood”, with regards to memory allocations?

  • You cannot declare a static variable within a nested class (I think the exact error was that static attributes can only be declared at the top level class). Why is this and what other restrictions are there for nested classes?

If possible, please say whether your answer is Java-specific, or whether C++ also follows the same rules?

  • 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-01T11:20:42+00:00Added an answer on June 1, 2026 at 11:20 am

    Inner classes are exactly the same as regular classes as far as memory and compilation are concerned. (Perhaps there is some difference in the way they’re managed in memory, but it’s not visible to the average Java developer) In fact, when you compile a class that has an inner class, you can see the compiled .class file as OuterClass$InnerClass.class. So as far as I know, the JVM treats them the same.

    As far as static variables are concerned, I’m not certain what the issue is. For example, this runs for me:

    public class Tester {
      public static void main(String[] args) {
        System.out.println(InnerTester.MY_STRING);
      }
    
      public class InnerTester {
        public static final String MY_STRING = "MY_STRING";
      }
    }
    

    EDIT

    As Jeffery pointed out, this does NOT compile:

    public class Tester {
      public static void main(String[] args) {
        System.out.println(InnerTester.MY_STRING);
      }
    
      public class InnerTester {
        public static String MY_STRING = "MY_STRING";
      }
    }
    

    The difference is, I had the first static variable listed as final.

    After thinking about this awhile, I agree with @Eugene. They allow static fields on inner classes to be final because then there’s no problem with initialization. The value cannot change, you just initialize it to its value. If, however, the static field is not final, you then require an instance of the outer class to be created in order to initialize the field, and static members can’t be tied to particular instances.

    I found this discussion on it as well.

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

Sidebar

Related Questions

I have a couple of questions about generic wildcards in Java: What is the
I have a couple of questions. 1) Why cannot we see system tables (like
I have a couple of questions relating to UIViewController: 1) When are each of
I have couple of questions regarding garbage collector in java. Q1.As far as I
I have couple questions regarding some C++ rules. Why am I able to call
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
I have a couple questions about exceptions. 1) when you hit a catch block,
I have a couple questions about submitting blue-tooth enabled apps on the iPhone. I
I have a couple questions about creating a object (2 values) and how to
i have a couple questions about an idea i have to manage a form

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.