Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
Why am I not able to access outer class data member through inner class reference?
class OuterClass1 {

    static private int a = 10;

    void get() {
        System.out.println("Outer Clas Method");
    }

    static class StaticInnerClass {
        void get() {
            System.out.println("Inner Class Method:" + a);
        }
    }

    public static void main(String args[]) {

        OuterClass1.StaticInnerClass b = new OuterClass1.StaticInnerClass();
        b.get();
        System.out.println(b.a);
        System.out.println(b.c);

    }
}

I know that static nested class can access outer class data members so why I am not able to access outer class variable throgh inner class referencebut can access it by directly using it in inner class as above?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Java language specification provides the following rules for accessing static fields:

◆ If the field is static:

  • The Primary expression is evaluated, and the result is discarded. [...]
  • If the field is a non-blank final, then the result is the value of the specified class variable in the class or interface that is the type of the Primary expression.

Note that the specification does not allow searching for static fields in other classes; only the immediate type of the primary expression is considered.

In your case the primary expression is simply b. It is evaluated, and its result is discarded with no observable effect.

The type of the primary expression b is OuterClass1.StaticInnerClass. Therefore, Java treats b.a as OuterClass1.StaticInnerClass.a. Since OuterClass1.StaticInnerClass class does not contain a static field a, the compiler produces an error.

When you access fields inside a method of the class, a different set of rules is in effect. When the compiler sees a in

System.out.println("Inner Class Method:" + a);

it searches the class itself, and continues to outer classes when the field is not there. That is where the compiler finds a, so the expression compiles correctly.

Note: Accessing static members through non-static expressions is a bad idea. See this Q&A for an explanation.

over 4 years ago · Santiago Trujillo Report

0

You haven't shown us an inner class, StaticInnerClass is not an inner class. There's no such thing in Java as a "static inner class" because an inner class by definition is a nested class that is not static. Only inner classes have direct access to containing types' members. Because StaticInnerClass is not an inner class, it does not have such access.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!