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!
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.
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.
[code=java]
class OuterClass{
static{
System.out.prin tln("Hello ...!!!!!");
}
static class InnerCLass{
}
}
public class StaticTest {
/** Creates a new instance of StaticTest */
public StaticTest() {
}
public static void main(String args[]){
OuterClass.Inne rCLass ic = new OuterClass.Inne rCLass();
}
}
[/code]
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.
[code=java]
class OuterClass{
static{
System.out.prin tln("Hello ...!!!!!");
}
static class InnerCLass{
}
}
public class StaticTest {
/** Creates a new instance of StaticTest */
public StaticTest() {
}
public static void main(String args[]){
OuterClass.Inne rCLass ic = new OuterClass.Inne rCLass();
}
}
[/code]
Please explain ...!
Sorry Josh when I ran this file then most probably it was not complied ..previous started running ....Sorry!
Anyway Thanks Josh!!!
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.
[code=java]
class OuterClass{
static{
System.out.prin tln("Hello ...!!!!!");
}
static class InnerCLass{
}
}
public class StaticTest {
/** Creates a new instance of StaticTest */
public StaticTest() {
}
public static void main(String args[]){
OuterClass.Inne rCLass ic = new OuterClass.Inne rCLass();
}
}
[/code]
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.
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.
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?
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.
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 ..... :-(
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.
Comment