• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

No inner class can have a static member. ??

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

I came across this line while reading the SCJP traps on Javaranch faq
No inner class can have a static member.

My question is what if I have a inner class inside a static method...or a static inner class...arent the members of such static inner classes automatically static?

Thanks for any help.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes , all nested classes cannot declare static member unless static classes
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by megha joshi:
Hi all,

I came across this line while reading the SCJP traps on Javaranch faq
No inner class can have a static member.

My question is what if I have a inner class inside a static method...or a static inner class...arent the members of such static inner classes automatically static?

Thanks for any help.



No. Members of a nested class, whether a static nested class or an inner class, are not automatically static.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

No inner class can have a static member.



No Inner class can have static members unless they are compile time constant fields.

Try to compile this:

class OuterClass{

class InnerClass{
static final int x = 23;
}

}


My question is what if I have a inner class inside a static method...or a static inner class...
arent the members of such static inner classes automatically static?




If you have a inncer class inside a static method, the inner class is not automatically static, this inner class is like any other objet made with the new operator, because you're declaring this, making an instance as any common class.


Try to compile this:


class OuterClass2 {

static void holdAInner() {

class InnerClass {
public void doSomething() {
System.out.println(".....");
}
}

InnerClass iObject = new InnerClass();

iObject.doSomething();
}
}

Regards...
[ April 24, 2007: Message edited by: Victor Velazquez ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[megha joshi]:

No inner class can have a static member.

My question is what if I have a inner class inside a static method...or a static inner class...arent the members of such static inner classes automatically static?


There is no such thing as a static inner class. By definition, an inner class is not static. When people mistakenly talk about static inner classes, they should say static nested classes instead.

Static nested classes can declare static members. Inner classes cannot. (Unless the field is final and initialized with a compile-time constant, as Victor said.)

An inner class may inherit a static field from a superclass. But it can't declare a static field.
 
The only cure for that is hours of television radiation. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic