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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:13:44+00:00 2026-05-27T15:13:44+00:00

Possible Duplicate: Why does Java prohibit static fields in inner classes? Let’s look at

  • 0

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

Let’s look at the following code snippet in Java. It just sums up two numbers within the Inner class declared inside the Outer class and works just fine as expected.

package staticfields;

final class Outer
{
    final public static class Inner
    {
        private static int x;
        private static int y;

        public Inner(int x, int y)
        {
            Inner.x=x;
            Inner.y=y;
        }

        public void sum()
        {
            System.out.println(x+y);
        }
    }
}

final public class Main
{
    public static void main(String[] args)
    {
        new Outer.Inner(5, 10).sum();
    }
}

When I attempt to remove the static keyword from the Inner class, it issues a compile-time error indicating that inner classes can not have static declarations means that the the static fields (x and y) declared within the Inner class don’t work, if it is made non-static.


Why do only static inner classes in Java have static members and non-static inner classes don’t?

  • 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-27T15:13:45+00:00Added an answer on May 27, 2026 at 3:13 pm

    An inner class is a nested class that is not explicitly or implicitly
    declared static. Inner classes may not declare static initializers
    (§8.7) or member interfaces. Inner classes may not declare static
    members, unless they are compile-time constant fields”

    • http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3

    Inner class belong to an instance of the out class, so it makes sense.

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

Sidebar

Related Questions

Possible Duplicate: Does Java guarantee that Object.getClass() == Object.getClass()? If I have a class
Possible Duplicate: Why does (360 / 24) / 60 = 0 … in Java
Possible Duplicate: What does this C++ code mean? I'm trying to map a C
Possible Duplicate: Java - why no return type based method overloading? The compiler does
Possible Duplicate: What is the Java ?: operator called and what does it do?
Possible Duplicate: When do you use Java's @Override annotation and why? Java, What does
Possible Duplicate: The constructor Date(…) is deprecated. What does it mean? (Java) I'm getting
Possible Duplicate: Why would one declare a Java interface method as abstract? The following
Possible Duplicate: What does “String[] args” contain in java? I want to know the
Possible Duplicate: Import package.* vs import package.SpecificType while importing packages in java does it

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.