Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda