• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

static final fields in inner class

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Within inner class static final variable(Line 1) is not giving any error but static final method(Line 2) is giving error --- why ?
If we make it as static inner class then Line 2 is not showing any error --- why?

Thanks in advance
 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to compile the code, both the Line 1 and Line 2 are giving below errors respectively

1. The field i cannot be declared static; static fields can only be declared in static or top level types
2. The method m cannot be declared static; static methods can only be declared in a static or top level type

I think the errors are quite self explanatory.
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way if what you have posted is the complete code, it has a syntax error.
The end curly brace is missing.
Probably you are using an IDE like eclipse, in which Line 1 is not throwing any error, but at the end of the line, it is showing


If you would have used command prompt to compile, you would have got the actual error, which is
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the rectified code ----
class Outer{
class Inner {
static final int i = 5; //Line 1
static final void m() { //Line 2
System.out.println("INNER");
}
}
}

Line-1 is not showing any error , Line 2 is showing error----- The method m cannot be declared static; static methods can only be declared in a static or top level type.
If you remove static from Line 2 then it compiles fine. But why static is not allowed here.
And in line 1 if we remove final then also error comes .Why?
 
Marshal
Posts: 80735
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you looked in the Java® Language Specification (=JLS)? It says inner classes can only have constants as static members. Careful: the JLS can be difficult to read.
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cambell,
I just went through the JLS. First of all it is very difficult to follow and understand.But what it says --

It is a compile-time error if an inner class declares a static initializer (§8.7).
It is a compile-time error if an inner class declares a member that is explicitly or implicitly static, unless the member is a constant variable (§4.12.4).

It's a rule.I am fine with that.My question is why this rule has made.

I am trying to figure it out and some ideas comes into my mind like constant variable initialize at the time of class loading but simple static member initialize when the class compiles
but the exact reason behind this rule I am not able to decide yet.

Thanks
Abhra
 
Campbell Ritchie
Marshal
Posts: 80735
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhra Kar wrote: . . . My question is why this rule has made. . . .

Don't know. Maybe somebody else will know.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason why you cannot have static (non-constant) members in non-static inner classes is because of the following.

An instance of a non-static inner class belongs to an instance of its enclosing class; it has a reference to the instance of its enclosing class that it belongs to. The following example demonstrates that.

As you can see, the Inner objects are created in the context of a specific Example object, and they can access the name member variable of the Example object that they belong to.

If it would be possible to add a static member variable to Inner, then it would be unclear what exactly this means. Should it mean that there is only one instance of that variable for all Inner classes, no matter what Example object they belong to? Or should it mean that there is one instance of that variable per Example object, which is shared between all Inner objects that are for the same Example object?

To avoid this ambiguity, the designers of the Java language decided that it should not be possible to have static members in a non-static inner class.
 
Campbell Ritchie
Marshal
Posts: 80735
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Jesper
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nice explanation Jesper

But I am not so sure how constants(Static final) will help this.Please explain regarding that point of view or refer me some link where I will get good ideas about java constants
(static final).


Thanks
Abhra
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And also why this static final m() is giving error.
 
Greenhorn
Posts: 2
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhra Kar wrote:

Within inner class static final variable(Line 1) is not giving any error but static final method(Line 2) is giving error --- why ?
If we make it as static inner class then Line 2 is not showing any error --- why?



Well, one plain reason is you can't use static in non-static. Second being, Inner class can have only compile-time constants. Hence you can use final static variables.
final static int i = 5; //allowed
static final void m() {} //not allowed, because it cannot be considered as a compile-time constant.
Go for static inner class if you want to 'i' and 'm' to be static or final or both.
 
Ranch Hand
Posts: 175
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhra Kar wrote:nice explanation Jesper

But I am not so sure how constants(Static final) will help this.


WHY CAN’T AN INNER CLASS DECLARE A NON-FINAL STATIC FIELD
A non-final static field is not associated with a specific instance, however, an inner class is associated with a specific instance. For this reason, declaring a non-final static field in an inner class is contradictory.

WHY CAN’T AN INNER CLASS DECLARE A FINAL OR NON-FINAL STATIC METHOD
Same explanation. A static method is not associated with a specific instance and therefore, is not passed an implicit this reference.

WHY CAN AN INNER CLASS DECLARE A FINAL STATIC FIELD
A final static field is not associated with a specific instance, however, an instance has a sense of ownership of the field i.e. it has its own “virtual copy” of the field and that “virtual copy” cannot be modified by another instance.
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many Thanks Joe for the explanation
 
reply
    Bookmark Topic Watch Topic
  • New Topic