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) {
            }
        }
    }
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 1
    Think about the lifetime of d, compared to the lifetime of an instance of class Inner. – Klitos Kyriacou Jun 08 '16 at 23:04
  • 1
    Although it is supported by the language, it's best not to define classes within methods... – Software Engineer Jun 08 '16 at 23:05
  • It should be accessible. What makes you think it's not? Are you running Java 8 or something older? – Sotirios Delimanolis Jun 08 '16 at 23:05
  • i got this question in a online test where they show d is not ..thats why.. – bhabajit kashyap Jun 08 '16 at 23:07
  • 1
    Did you try compiling this code before asking here? What research did you do? – Sotirios Delimanolis Jun 08 '16 at 23:07
  • @EngineerDollery That's a very broad statement. Just because it doesn't have a common use case, I don't think that makes it a Bad Thing. – shmosel Jun 08 '16 at 23:12
  • "Doesn't have a common use case"? What about all those anonymous classes for event handlers etc? – Klitos Kyriacou Jun 08 '16 at 23:13
  • Anonymous inner classes have a different structure -- perhaps I should have been clearer. However, even those are simply a way of old java getting around its lack of function pointers. This is fixed (finally) in java 8 so the ugly and not-really-OO technique of writing AICs to handle events is now dying out in favour of lambdas. So declaring classes of extremely restricted scope is not necessary as often any more. There may still be some edge cases, but it is a bad idea and you should be questioning yourself if you do it. – Software Engineer Jun 08 '16 at 23:23
  • I just tried it in Eclipse and I could access `d` just fine. – Pace Jun 08 '16 at 23:45
  • Actually, I can access `d` if it is Java 8, but not on Java 7 or earlier. – Pace Jun 08 '16 at 23:47
  • See http://stackoverflow.com/questions/23580826/local-class-can-access-non-final-variable-in-java-8 – Pace Jun 08 '16 at 23:48

2 Answers2

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

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
54l3d
  • 3,913
  • 4
  • 32
  • 58