• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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.
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic