• 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

static class question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
Can a static class be instantiated?
If no, the example below "new Test.Inner().go()" are creating Test instance or Inner instance?
Thanks


 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yeung Charles wrote:Can a static class be instantiated?



yes, nested class can be instantiated
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
welcome to javaranch
 
Yeung Charles
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:welcome to javaranch



so "new Test.Inner().go();" is creating a instance of Inner because Inner() not Test()?

But what is the meaning "you don’t
use an instance of the outer class. You just use the
name of the class"?

Original sentence from Head First Java (Because a static nested class is...static, you don’t
use an instance of the outer class. You just use the
name of the class, the same way you invoke static
methods or access static variables.)

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yeung Charles wrote:But what is the meaning "you don’t
use an instance of the outer class. You just use the
name of the class"?


Precisely what it says--you're not using an instance of the outer class. If you access a static class variable you're not using an instance of the class, like Foo.A_CONSTANT_FROM_FOO.
 
Yeung Charles
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:

Yeung Charles wrote:But what is the meaning "you don’t
use an instance of the outer class. You just use the
name of the class"?


Precisely what it says--you're not using an instance of the outer class. If you access a static class variable you're not using an instance of the class, like Foo.A_CONSTANT_FROM_FOO.



So, when calling go(), we use new Test.Inner().go() instead of Inner.Test.go() because go() is not declared in static.
If it is correct, then "new Test.Inner().go()" = create an instance of Inner and call go()?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You couldn't use Inner.Test.go() because that method doesn't exist.
 
Yeung Charles
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:You couldn't use Inner.Test.go() because that method doesn't exist.



I know, I am sorry for the typing mistake, it should be Test.Inner.go().
 
Yeung Charles
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another question that why line 3 is ok and line 4 is fail?
What is the difference between "static final" and "static" in "inner class"?

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic