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 9, 2018 in Java by 93.lynn
• 1,600 points
1,814 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 9, 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
1,066 views
0 votes
2 answers

When to use Static Methods in Java?

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

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
16,067 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,140 points
1,929 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,920 points
4,022 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,920 points
1,147 views
0 votes
2 answers

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

A static keyword  can be used with ...READ MORE

answered Jun 11, 2019 in Java by Neha
• 330 points
1,481 views
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
• 59,230 points
2,069 views
0 votes
1 answer

Static Classes In Java

Java has static nested classes but it sounds like ...READ MORE

answered Dec 23, 2020 in Java by Gitika
• 65,770 points

edited Jul 5, 2023 by Khan Sarfaraz 923 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 28, 2018 in Java by anto.trigg4
• 3,440 points
1,254 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 21, 2019 in Java by Roberto
• 460 points

edited Jul 7, 2020 by Roberto 38,398 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP