1

why d is not accessible from the class Inner and b is accessible from the same class ?

public class Outer {
    public int a = 1;
    private int b = 2;

    public void method(final int c) {
        int d = 3;
        class Inner {
            private void iMethod(int e) {
            }
        }
    }
}
11
  • 1
    Think about the lifetime of d, compared to the lifetime of an instance of class Inner. Commented Jun 8, 2016 at 23:04
  • 1
    Although it is supported by the language, it's best not to define classes within methods... Commented Jun 8, 2016 at 23:05
  • It should be accessible. What makes you think it's not? Are you running Java 8 or something older? Commented Jun 8, 2016 at 23:05
  • i got this question in a online test where they show d is not ..thats why.. Commented Jun 8, 2016 at 23:07
  • 1
    Did you try compiling this code before asking here? What research did you do? Commented Jun 8, 2016 at 23:07

2 Answers 2

1

It is because a and b are properties of the class Outer whereas d is a variable inside the method called method. So it should have access to d as well.

Here is an example from tutorialspoint.

In Java, we can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted within the method.

A method-local inner class can be instantiated only within the method where the inner class is defined. The following program shows how to use a method-local inner class.

public class Outerclass{

//instance method of the outer class 
 void my_Method(){
  int num = 23;

  //method-local inner class
  class MethodInner_Demo{
     public void print(){
        System.out.println("This is method inner class "+num);     
     }   
  }//end of inner class

  //Accessing the inner class
  MethodInner_Demo inner = new MethodInner_Demo();
  inner.print();
}

public static void main(String args[]){
  Outerclass outer = new Outerclass();
  outer.my_Method();           
 }
}

This is method inner class 23

Sign up to request clarification or add additional context in comments.

1 Comment

@SotiriosDelimanolis Corrected the answer.
1

Inner is a local class, because it's declared locally within a block of Java code, rather than as a member of a class. Therefore Inner can access any members like a and b, including private members, of the containing class Outer. And it cannot access d because d is not declared final, for Java versions 7 and older.

[..] a local class can use the local variables, method parameters, and even exception parameters that are in its scope, but only if those variables or parameters are declared final. This is because the lifetime of an instance of a local class can be much longer than the execution of the method in which the class is defined. For this reason, a local class must have a private internal copy of all local variables it uses (these copies are automatically generated by the compiler). The only way to ensure that the local variable and the private copy are always the same is to insist that the local variable is final.

See here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.