File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes static block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static block" Watch "static block" New topic
Author

static block

abhinas raj
Ranch Hand

Joined: Jun 02, 2012
Posts: 47
How to call static block more than once in a single class ?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jun 02, 2012
Posts: 47
through separate classloaders would you please write the code for that
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7801
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
abhinas raj
Ranch Hand

Joined: Jun 02, 2012
Posts: 47
it was an interview question. so answer should be like we can not call more than once ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7801
    
  21

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jul 27, 2010
Posts: 1157

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.


~ Mansukh
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static block