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,620 points
411 views

1 answer to this question.

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,440 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,300 points
157 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,200 points
418 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,110 points
607 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,890 points
845 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,890 points
147 views
0 votes
2 answers
0 votes
2 answers

Why it is not possible to define a static method in a Java interface?

Interfaces are concerned with polymorphism which is ...READ MORE

answered Aug 27, 2019 in Java by Sirajul
• 58,130 points
272 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,440 points
268 views
+1 vote
2 answers

Retrieve all the implementations of an interface in Java

Take a look to this example: https://github.com/burningwave/co ...READ MORE

answered Dec 20, 2019 in Java by Roberto
• 370 points

edited Jul 6 by Roberto 9,905 views