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,560 points
65 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,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,300 points
47 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
90 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,050 points
45 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,870 points
77 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,870 points
49 views
0 votes
2 answers
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,540 points
52 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
95 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
395 views