• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

problem in static block of a class

 
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
i have a class which has a static block and a static method.For the first time i call the static method in the class its strange that its excuting a part of the static block and entering the method later and agian turning back to static block.
but for the secon time when i call the static method in that class its fine that static block is not executed..
here is the code of the class which contains static block and static method
public class LogServices
{
private static String propFilename="plas.properties";
private static String file="vdvd";
static
{
System.out.println("Inside static block");
try
{
System.out.println("Inside static block of Plasma Log Services ");
file=PropertyProvider.getProperty(propFilename,"Log4jprops"); // here i am calling another method in another class to get some string from properties
System.out.println("\n File is ********************************** "+file);
}
catch(Exception e)
{
System.out.println("Exception encountered the following Exception ["+e.getMessage()+"]");
}
System.out.println("Finished static block");
}
public static Logger initLog()
{

Logger logger=Logger.getLogger(PlasmaLogServices.class);

System.out.println("File in method is:"+file);
return logger;
}
}
and in my main program i am calling this
LogServices.initLog(); and the output i observed on the screen is
---------------------------------------------
Inside static block
Inside static block of Plasma Log Services
File in method is:vdvd
File is ********************************** log.properties
Finished static block
------------------------------------------
if u look at the output after " Inside static block of Plasma Log Services" (part of static block) its enetering method where " File in method is:vdvd" is printed .
After some debugging i came to know that if i remove the line
file=PropertyProvider.getProperty(propFilename,"Log4jprops"); and replace it by file="log.properties";(mean hardcoded) then the static block executes and later that method executes.

can any one pls help me in this regard???
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmmmmmm ... I double-checked the JVM spec regarding Static Initializers and it implies that the static initializers will all run BEFORE anything else happens (see section 2.17.4 and others). Mostly.
When you instaniate a class, or invoke a static method, the JVM will lock the class object and check to see if it has been initialized. If not, it will release the lock and run the static initializers in the order they are defined. Then it will return to whatever else it was doing. However the static initializers are run only once: when the class is first loaded.
I ran several tests and my static initializer ALWAYS ran to completion before anything else happened, regardless of how long it took. With one exception: If I spawned a separate thread within the "static { }" block then the static initializer would finish and execution would continue even though the other thread was still running. There may be other ways of introducing this behaviour, but that was the only one I found.
So, just out of curiosity, the method you are calling, "PropertyProvider.getProperty(...)", does that do anything "strange" in terms of threading or anything else that might by synchronized?
Another possibility--related to multi-threading--is that the output is displayed in a different order in which it is written out. I've seen this when two separate thread are both doing "System.out.println()" calls, and the order you see may be different than the order in which they were actually written.
I'd like to hear if anybody else has an idea on this topic ...
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic