Connecting Tech Pros Worldwide Forums | Help | Site Map

Why inner class does not have static block?

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#1: Jul 26 '08
Yesterday night i was learning inner class there i came across a thing that Inner Class does not support Static Block ...
I need the explanation ..Please!

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Jul 26 '08

re: Why inner class does not have static block?


Quote:

Originally Posted by dmjpro

Yesterday night i was learning inner class there i came across a thing that Inner Class does not support Static Block ...
I need the explanation ..Please!

Inner classes don't need static blocks because they can use the static blocks
of the outer class that embraces them.

kind regards,

Jos
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#3: Jul 26 '08

re: Why inner class does not have static block?


Quote:

Originally Posted by JosAH

Inner classes don't need static blocks because they can use the static blocks
of the outer class that embraces them.

kind regards,

Jos

Thanks :-)
That means if inner class loaded then enclosing class gets loaded.
But this code snippet is not working properly, means outer class static block is not running.
Expand|Select|Wrap|Line Numbers
  1. class OuterClass{
  2.     static{
  3.         System.out.println("Hello ...!!!!!");
  4.     }
  5.     static class InnerCLass{
  6.  
  7.     }
  8. }
  9.  
  10. public class StaticTest {
  11.  
  12.     /** Creates a new instance of StaticTest */
  13.     public StaticTest() {
  14.     }
  15.  
  16.     public static void main(String args[]){
  17.         OuterClass.InnerCLass ic = new OuterClass.InnerCLass();
  18.     }
  19. }
  20.  
Please explain ...!
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#4: Jul 26 '08

re: Why inner class does not have static block?


Quote:

Originally Posted by dmjpro

Thanks :-)
That means if inner class loaded then enclosing class gets loaded.
But this code snippet is not working properly, means outer class static block is not running.

Expand|Select|Wrap|Line Numbers
  1. class OuterClass{
  2.     static{
  3.         System.out.println("Hello ...!!!!!");
  4.     }
  5.     static class InnerCLass{
  6.  
  7.     }
  8. }
  9.  
  10. public class StaticTest {
  11.  
  12.     /** Creates a new instance of StaticTest */
  13.     public StaticTest() {
  14.     }
  15.  
  16.     public static void main(String args[]){
  17.         OuterClass.InnerCLass ic = new OuterClass.InnerCLass();
  18.     }
  19. }
  20.  
Please explain ...!

Sorry Josh when I ran this file then most probably it was not complied ..previous started running ....Sorry!
Anyway Thanks Josh!!!
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Jul 26 '08

re: Why inner class does not have static block?


Quote:

Originally Posted by dmjpro

Thanks :-)
That means if inner class loaded then enclosing class gets loaded.
But this code snippet is not working properly, means outer class static block is not running.

Expand|Select|Wrap|Line Numbers
  1. class OuterClass{
  2.     static{
  3.         System.out.println("Hello ...!!!!!");
  4.     }
  5.     static class InnerCLass{
  6.  
  7.     }
  8. }
  9.  
  10. public class StaticTest {
  11.  
  12.     /** Creates a new instance of StaticTest */
  13.     public StaticTest() {
  14.     }
  15.  
  16.     public static void main(String args[]){
  17.         OuterClass.InnerCLass ic = new OuterClass.InnerCLass();
  18.     }
  19. }
  20.  
Please explain ...!

That class is not an inner class; it's just a static nested class. If you'd leave out
the 'static' it would've been an inner class.

kind regards,

Jos
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#6: Jul 26 '08

re: Why inner class does not have static block?


Quote:

Originally Posted by JosAH

That class is not an inner class; it's just a static nested class. If you'd leave out
the 'static' it would've been an inner class.

kind regards,

Jos

I think the same thing happens to static nested class and inner class. :-)
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#7: Jul 26 '08

re: Why inner class does not have static block?


Quote:

Originally Posted by dmjpro

I think the same thing happens to static nested class and inner class. :-)

No, static nested classes can have static blocks unlike inner classes. A static
nested class belongs to the outer class while an inner class belongs to an
instantiation (an object) of the outer class.

kind regards,

Jos
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#8: Jul 26 '08

re: Why inner class does not have static block?


Quote:

Originally Posted by JosAH

No, static nested classes can have static blocks unlike inner classes. A static
nested class belongs to the outer class while an inner class belongs to an
instantiation (an object) of the outer class.

kind regards,

Jos


What would happen if Inner Class supports Static block?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#9: Jul 26 '08

re: Why inner class does not have static block?


Quote:

Originally Posted by dmjpro

What would happen if Inner Class supports Static block?

It doesn't support static blocks; an instantiation of an inner class belongs to an
instantiation of the outer class; like planets rotating around a star; the Earth would
be an instantiation of the inner class Planet which has an outer class Star and
an instantiation Sun; that makes an Earth rotate around a Sun. Everything static
to Earth would just be an element of the object Sun which isn't static but just a
member of that outer class. If you want something 'more static' you can use the
static members of the Star class; you don't need a separate static block for that.

kind regards,

Jos
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#10: Jul 26 '08

re: Why inner class does not have static block?


Quote:

Originally Posted by JosAH

It doesn't support static blocks; an instantiation of an inner class belongs to an
instantiation of the outer class; like planets rotating around a star; the Earth would
be an instantiation of the inner class Planet which has an outer class Star and
an instantiation Sun; that makes an Earth rotate around a Sun. Everything static
to Earth would just be an element of the object Sun which isn't static but just a
member of that outer class. If you want something 'more static' you can use the
static members of the Star class; you don't need a separate static block for that.

kind regards,

Jos

It would be more helpful if you please explain this line ...
Everything static
to Earth would just be an element of the object Sun which isn't static but just a
member of that outer class

Sorry for inconvenience ..... :-(
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#11: Jul 26 '08

re: Why inner class does not have static block?


Quote:

Originally Posted by dmjpro

It would be more helpful if you please explain this line ...
Everything static
to Earth would just be an element of the object Sun which isn't static but just a
member of that outer class

Sorry for inconvenience ..... :-(

It was an analogy: whatever seems static for Earth, Mars, Venus, Mercury or
whatever planet rotating around the Sun would simply be an instance member
of that Sun object; which in turn would simply be an *instance* of the Star class.

Of course static members of the Star class can be freely used by instances of
the Planet class.

kind regards,

Jos
Reply