0

In Java we cannot declare static initializers or member interfaces in a local class (static members can be declared provided there are final and can be initialized at compile time). My question is why? What is the rationale behind this design decision?

Thanks

BestCoderEver
  • 467
  • 1
  • 7
  • 15
  • 3
    By "local class" do you mean inner class? – Brett Okken Jan 15 '15 at 03:24
  • 4
    I don't know that this question can be answered *unless* James Gosling happens to stroll by. Have you considered looking at the bytecode with `javap -v`? – Elliott Frisch Jan 15 '15 at 03:24
  • How should we know the answer to this? – Radiodef Jan 15 '15 at 03:35
  • @Radiodef Assuming that this design decision was based on some rationale it is possible that not only author of this decision was able to think about this rationale, so I think it is interesting question. Because of questions like this I love Stack Overflow, it is a pity that there are not so many of them. – Pshemo Jan 15 '15 at 03:38
  • Jarrod Roberson, can you please provide the link, i can't find any. My question is on the rationale.. and not what local classes are (i have the spec for that) – BestCoderEver Jan 15 '15 at 03:44
  • No, he does not mean inner classes. He means local classes. If he meant inner classes, he would likely have said inner classes. If you don't know what a local class is, read this. http://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html – Keith Irwin Jan 15 '15 at 03:46
  • @Jarrod, why is a question about local classes marked as a duplicate of a question about inner classes? – Keith Irwin Jan 15 '15 at 03:48
  • local classes are a type of inner classes in Java –  Jan 15 '15 at 06:01

4 Answers4

1

Local classes can only be accessed from inside the method or scope block in which they are defined.

static initializer or interface make no sense in that context

user2649908
  • 557
  • 7
  • 15
1

I think inner classes are non-static by definition because they can access non-static members of the class they are contained in.

This means that any "static methods" of this class would, in essence, be non-static as they could differ based on which object they are contained in.

For example:

public class Container {
    public int x;
    public class Contained {
        static int x = Container.this.x;
    }
}

If this compiled, you could do this:

Container a = new Container();
a.x = 1;
Container b = new Container();
b.x = 2;

Then a.Contained.x != b.Contained.x (assuming this line could compile), which doesn't make sense since both are supposed to be static.

k_g
  • 4,333
  • 2
  • 25
  • 40
  • This is hardly a compelling reason. A static initializer can't access instance members to begin with. – Radiodef Jan 15 '15 at 03:34
  • Oh, I see what you mean. If the static method can't access instance methods of the container class, then it basically _is_ in a static class. I think the design philosophy was that you should just define a static class if you want that type of behavior – k_g Jan 15 '15 at 03:36
  • To elaborate, I had assumed that you meant that the static initalizers/methods could access instance methods from the container (because the class isn't defined independently of the object it is contained in) – k_g Jan 15 '15 at 03:38
1

Actually local classes, if non-static, are members of the class that contains it. And if a class itself is non-static, it's instances are contained within the instances of the class that contains it(technically, they referred using instances of main class that contains it). And so having static initializers make no sense here in a local class. As n number of instances of the local class cannot share stuff as they are always discrete.

Aditya Singh
  • 2,343
  • 1
  • 23
  • 42
0

I don't know for sure, but my best guess is that they chose for them not to because the timing would be problematic. In normal classes, static initializers run prior to any other code at all running. They run prior to any members of any sort running at all. A static initializer in the local class needs to have access to local variables, but obviously those do not exist until the enclosing code is run. They could potentially have chosen to have local classes have their initializers run when the enclosing block executes, but they may have feared that because this is entirely different from the timing of normal classes that it would either create programmer confusion about when static initializers of local classes are run or that it might cause technical problems in the virtual machine due to having to special-case when those classes static members are run.

Keith Irwin
  • 5,628
  • 22
  • 31