• Post Reply Bookmark Topic Watch Topic
  • New Topic

static class question  RSS feed

 
Yeung Charles
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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


 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome to javaranch
 
Yeung Charles
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.)

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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"?

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!