Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block

 
abhinas raj
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to call static block more than once in a single class ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhinas raj wrote:How to call static block more than once in a single class ?


The static initializer block is only called when the class is loaded. So the only way to call it more than once is to load the class multiple times through separate classloaders.

If you want to invoke that code on demand, put it in a method. That's what they're for.
 
abhinas raj
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
through separate classloaders would you please write the code for that
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhinas raj wrote:through separate classloaders would you please write the code for that

No. We are NotACodeMill (←click).

And why would you want to call a static block more than once?

Winston
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
abhinas raj wrote:through separate classloaders would you please write the code for that

No. We are NotACodeMill (←click).

And why would you want to call a static block more than once?


As an FYI... Writing class loaders is not something that is done often. In fact, I will argue that most Java programmers will never have the need to do it -- ever.

Henry
 
abhinas raj
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it was an interview question. so answer should be like we can not call more than once ?
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhinas raj wrote:it was an interview question. so answer should be like we can not call more than once ?

Well, plainly that's not right, since Jeff already told you how you can do it. I suppose what you could say is that without multiple ClassLoader's you can't call it more than once.

You could also say that that you can't actually "call" a static block at all. It's executed for you by the action of loading a class.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhinas raj wrote:it was an interview question. so answer should be like we can not call more than once ?


The answer is exactly what I already stated (subject to Winston's correction that we don't actually call it ourselves anyway). This sounds like one of those questions where the interviewer thinks he's being "clever," so nobody here knows what answer he was actually looking for.

Rather than trying to memorize the answers to every possible interview question, you'll do a lot better if you just actually learn the fundamentals of Java.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: This sounds like one of those questions where the interviewer thinks he's being "clever," so nobody here knows what answer he was actually looking for.


Well, my observation about the interviewers just got reinforced. What is meant by a class loader Jeff? When is a class loaded in the JVM? When we compile it or when we actually run it?

Jeff Verdegan wrote: Rather than trying to memorize the answers to every possible interview question, you'll do a lot better if you just actually learn the fundamentals of Java


Period.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:What is meant by a class loader Jeff?


http://www.google.com/search?q=java+classloader

When is a class loaded in the JVM? When we compile it or when we actually run it?


I write some Java code. I compile it to .class files and put them into a .jar. A week later I post that jar file on the web, from my house here in California, and maybe the server is in New York. Another week after that, you buy a brand new computer, and once you get it set up, you download my jar file to your computer in India. You start up a JVM and execute my jar file.

Do you think my class got loaded into your JVM in India when I compiled it in California, 2 weeks before you got your computer?

Or do you think it got loaded into your JVM when you ran the JVM and told it to execute my jar?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic