• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubts regarding static initializer  RSS feed

 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

when class Test is run it gives output "QBANK" .
My Doubt is Static Initializer runs when class is loaded .
Since the class Sub is loaded as you can verify through the command " java -verbose Test",
the output should include " In Sub" along with "QBANK"
can anybody please explain me?

thanks in advance ..

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your main() try out
Now, can you think why you were getting "QBANK" ?

Ok found it (finally)
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#39245
Any static initializers declared in a class are executed when the class is initialized and,...
 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the meaning of class in initialized
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish Singh wrote:what is the meaning of class in initialized
Does this Java™ language Specification section help?
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The missing link must be the "new" operator. The static initialization block
that prints "In Sub" does not run until a Sub object is created.

Jim ... ...
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. Well, there are several kinds of events that could have caused the class to be initialized - one of them is the creation of an instance of the class. A complete list of these events occurs in the third paragraph of the section Campbell just linked to.
 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Campbell Ritchie
thanks
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!