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,600 points
128 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
91 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
134 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,080 points
105 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,880 points
271 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,880 points
69 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
• 45,310 points
118 views
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,560 points
88 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
140 views
+1 vote
2 answers

Retrieve all the implementations of an interface in Java

Take a look here: https://github.com/burningwave/core/wiki/USE- ...READ MORE

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

edited Dec 31, 2019 by Roberto 2,112 views