• 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

tricky question

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai ranchers can any one expalain about this question


class Super { static String ID = "QBANK"; }
class Sub extends Super
{
static { System.out.print("In Sub"); }
}
class Test
{
public static void main(String[] args)
{
System.out.println(Sub.ID);
}
}

ans is QBANK

by velan vel
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi velan,

i think that the static block in class Sub will only be executed when one tries to make an instance of it. By merely using the Class Name to get value of it's member will not result in execution of it's static block.

if one thinks purpose of static keyword, it becomes clear:
The syntax
is known as static initializer . Now here no code is doing initialization of Class Sub. So the string "In Sub" won't be printed.

Ranchers, please let me know if i am incorrect.
--Harshil
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harshil,
I tried executing following code:-
class Super { static String ID = "QBANK";
static {
System.out.println("In Super");
}
}
class Sub extends Super
{
Sub(){
System.out.print("In Sub Constructor");

}
static { System.out.print("In Sub");
}

}
class Test3
{
public static void main(String[] args)
{
System.out.println(Sub.ID);
}
}
Output of this is
InSuper
QBANK

Static block in subclass does not get executed but static block in super gets executed.
Can you please give any comments on this?
Regards,
Poonam
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please correct me if i am wrong.
I think if class A extends B.the compiler first loads the class B
before it compiles A,so the static blocks of B are being initialised.
and in the above code if the static ID is moved to class Sub the static blocks of sub is executed.
so we can conclude that if static ID is in super its ok,else the sub is loaded by compiler which initialises the static blocks.

class Super {
//static String ID = "QBANK";
static{
System.out.print("In Super");
}
}
class Sub extends Super
{
static { System.out.print("In Sub"); }
static String ID = "SUBQBANK";
}
public class TimDig
{
public static void main(String[] args)
{
System.out.println(Sub.ID);

}
}
Please correct me if i am wrong.
thanks,
santosh
[ December 29, 2005: Message edited by: santosh kothapalli ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The reason might be that we are not instantiating the Sub class, just referring the static member of the class, now all the static members are clubbed with the existing static init block and executed simulteneously. So here the calling of the static member triggered the remaining static statements in 'Super' class.

Since we are not touching the member of 'Sub' class, it's static block is not executed!

Pl do correct me if I am wrong.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#39245

The class Super is initialized while Sub is not initialized eventhough we call Sub.id. That means initialization is done on the class which have the variable referenced.

I will dig deep into this I still don't know why Sub is not initialized. It will have to do something with variable initialization than static block initialization.
 
Ranch Hand
Posts: 354
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sub is not initialized becuase it is never used. Static members are accessed via the reference of the class to which they belong.

A reference to a class field causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.


Straight out of JLS
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jack.

And thanks for the question.
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there wont be any question on initialiser blocks

so stay relaxed
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why would there be no questions in static initialiser block. It is in the syllabus of SCJP 1.4
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think you should read this post by Bert Bates then you will agree ???

nieranjan
 
reply
    Bookmark Topic Watch Topic
  • New Topic