Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > mysterious static

Reply
Thread Tools

mysterious static

 
 
Timo Nentwig
Guest
Posts: n/a
 
      06-13-2005
Hi!

I'm somewhat confused:

class Foo
{
private static final ConditionalPropertiesReader PROPERTIES =
ConditionalPropertiesReader.getInstance();
private static final String defaultCacheStrategy =
PROPERTIES.getProperty( "jcms.cache.strategy" );

static
{
System.out.println("1 static{}"+defaultCacheStrategy);
}
....
public static IJCMSCacheStrategy getDefaultCacheStrategy()
{
System.out.println("2 getDefaultCacheStrategy()
"+defaultCacheStrategy);

return JCMSCacheStrategy.Factory.make( defaultCacheStrategy != null
? defaultCacheStrategy
:
JCMSCacheStrategy.Factory.CACHESTRATEGY_DEFAULT );
}
}


This prints:
2 getDefaultCacheStrategy() null
1 static{} myvalue


Why is that?? If I create an instance of the class prior to calling the
method defaultCacheStrategy in getDefaultCacheStrategy() *does* have the
supposed value. And why is 2 printed first??

Can somebody explain?
 
Reply With Quote
 
 
 
 
Timo Nentwig
Guest
Posts: n/a
 
      06-13-2005
Timo Nentwig wrote:

> This prints:


If I call Foo.getDefaultCacheStrategy() from another class, of course
 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      06-14-2005
Timo Nentwig wrote:

> This prints:
> 2 getDefaultCacheStrategy() null
> 1 static{} myvalue


Unless I'm mis-reading your code, it /shouldn't/ do that. The static
initialisation should have completed before the static method is entered.
Possibly there's something wrong with your real code that doesn't appear in the
snippet you posted; can you create a complete self-contained example ?

-- chris



 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      06-14-2005
Chris Uppal wrote:

> Timo Nentwig wrote:
>
>
>>This prints:
>>2 getDefaultCacheStrategy() null
>>1 static{} myvalue

>
>
> Unless I'm mis-reading your code, it /shouldn't/ do that. The static
> initialisation should have completed before the static method is entered.
> Possibly there's something wrong with your real code that doesn't appear in the
> snippet you posted; can you create a complete self-contained example ?


I agree that the posted code does not explain the observed behavior, but
I have a guess as to how it's happening. Class initializers -- the
combination of static variable initialization expressions and static
initializer blocks -- are executed at runtime in the order they appear
in the class' code. My bet would be that the getDefaultCacheStrategy()
method is invoked within the scope of some method call in one of the
initializers preceding the static initializer block shown. Because the
class is at that point already in the process of initialization, the
method call proceeds without attempting to start a new initialization,
and the unexpected execution order and default value of
defaultCacheStrategy are observed. This would be akin to a superclass
constructor invoking a virtual method, and thereby exposing a subclass'
instance variable before it has been initialized by the subclass
constructor.

Schematic sequence:

() class Foo is uninitialized at the beginning of the sequence

() Foo.getDefaultCacheStrategy() is invoked

() the VM loads class Foo, and begins initializing it

() static member baz is initialized by invoking (hypothetical) static
method Foo.getCache()

() Foo.getCache() invokes Foo.getDefaultCacheStrategy(), which prints
the observed message containing the default value for the
defaultCacheStrategy variable

() static member defaultCacheStrategy is initialized via its initializer

() the VM runs the static initializer block, which prints the second
message (numbered 1) which includes the initialized value of
defaultCacheStrategy

() initialization of Foo is complete, execution of
Foo.getDefaultCacheStrategy() begins (for the second time)

() Another message numbered 2 is printed (not reported by the OP)


That's the only thing I can think of. A class method cannot ever be
invoked before its class is initialized, but it can be invoked *while*
its class is being initialized, in which case it will see a partially
initialized class.

--
John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Timo Nentwig
Guest
Posts: n/a
 
      06-14-2005
Chris Uppal wrote:

> Timo Nentwig wrote:
>
>> This prints:
>> 2 getDefaultCacheStrategy() null
>> 1 static{} myvalue

>
> Unless I'm mis-reading your code, it /shouldn't/ do that. The static


Yes, I found the bug today:

class Foo
{
// the bug:
private static final DEFAULT_INSTANCE = new Foo();

private static final ConditionalPropertiesReader PROPERTIES =
ConditionalPropertiesReader.getInstance();
private static final String defaultCacheStrategy =
PROPERTIES.getProperty( "jcms.cache.strategy" );
....

// I have to put the singleton stuff here
// as it depends on the lines above
}
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      06-15-2005
Timo Nentwig wrote:

> class Foo
> {
> // the bug:
> private static final DEFAULT_INSTANCE = new Foo();


Aha!

That's really quite a cute little bug

-- chris


 
Reply With Quote
 
Timo Nentwig
Guest
Posts: n/a
 
      06-15-2005
Chris Uppal wrote:

> That's really quite a cute little bug


Isn't it? I must admit I'm somewhat proud of it indeed
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tracking Mysterious Reload thefakemontezz@yahoo.com Cisco 4 03-17-2006 05:17 PM
Mysterious reappearing bbc.co.uk cookie (Firefox 1.5.0.1, WindowsXP Pro) Z Firefox 9 03-07-2006 03:03 AM
mysterious asp.net errors rh ASP .Net 3 05-03-2004 04:02 PM
Mysterious problem with forms.... Ivan Demkovitch ASP .Net 0 10-29-2003 10:58 PM
Mysterious cookie problem Bruce W.1 ASP .Net 1 07-29-2003 07:04 PM



Advertisments