Can't access static fields in inner classes in Java

0 votes

I have the below code where I am trying to access the static field with Outer_Class.Inner_Class.i as I want to record a static value in it. But Java doesn't allow static fields/methods in inner classes?. Can someone explain why so?

class Outer_Class {
 class Inner_Class {
  static int i = 100; // compile error
  static void f() { } // compile error
 }
} 
Oct 8, 2018 in Java by 93.lynn
• 1,550 points
42 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Well, the major idea behind this concept is, an inner class is a nested class that is not explicitly or implicitly declared static. So, the inner classes may not declare static initializers or member interfaces. Also, the inner classes may not declare static members, unless they are compile-time constant fields.

answered Oct 8, 2018 by anto.trigg4
• 3,420 points

Related Questions In Java

0 votes
1 answer

Static methods can't be abstract in Java

Well, Java doesn't allow this because of ...READ MORE

answered Oct 12, 2018 in Java by v.liyyah
• 1,290 points
36 views
0 votes
2 answers

When to use Static Methods in Java?

A static method has two main purposes: For ...READ MORE

answered Aug 9, 2018 in Java by samarth295
• 2,190 points
58 views
0 votes
1 answer

Can Static methods be inherited in java?

A subclass inherits all of the public ...READ MORE

answered Apr 30, 2018 in Java by Daisy
• 8,020 points
33 views
+1 vote
2 answers

What are inner classes and static nested classes?

Nested classes are divided into two categories: ...READ MORE

answered Dec 3, 2018 in Java by Sushmita
• 6,820 points
26 views
0 votes
2 answers

Does Java allow to create static classes?

Java has "static nested classes", but they ...READ MORE

answered Sep 5, 2018 in Java by Sushmita
• 6,820 points
40 views
0 votes
1 answer

Why are you not able to declare a class as static in Java?

Declare your class like this: public class eOAuth ...READ MORE

answered Oct 4, 2018 in Java by Daisy
• 8,020 points
33 views
0 votes
1 answer
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,520 points
34 views
0 votes
1 answer

Non-static method within Static method in Java

As per my knowledge, you are getting this error ...READ MORE

answered Sep 27, 2018 in Java by anto.trigg4
• 3,420 points
61 views
0 votes
1 answer

Retrieve all the implementations of an interface in Java

Hi...I think you can achieve this by ...READ MORE

answered Jul 23, 2018 in Java by anto.trigg4
• 3,420 points
118 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.