• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

static block execution

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,



My only doubt runs around
One o = null;
Under what cases static block of the class One will be executed or can I say , class One will be loaded?

This is the explanation to the answer that is I think pretty well described but I am not able to draw the case I have done in BOLD:



A class or interface type T will be initialized at its first active use, which occurs if:
T is a class and a method actually declared in T (rather than inherited from a superclass) is invoked.
T is a class and a constructor for class T is invoked, or U is an array with element type T, and an array of type U is created.
A non-constant field declared in T (rather than inherited from a superclass or superinterface) is used or assigned.






Source: Enthuware


Thanks in advance,
[ May 06, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



A class or interface type T will be initialized at its first active use, which occurs if:
T is a class and a method actually declared in T (rather than inherited from a superclass) is invoked.
T is a class and a constructor for class T is invoked, or U is an array with element type T, and an array of type U is created.
A non-constant field declared in T (rather than inherited from a superclass or superinterface) is used or assigned.



The static block will also get executed when there is an uninitialized static const and which is being assigned in the static block. When this constant is accessed, the static block will also get executed.

[ May 06, 2007: Message edited by: M Krishnan ]
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> or can I say , class One will be loaded?

seems there is a difference between loaded and initialized.


won't print "one"


also print nothing

Anyway, this is very interesting topic. Thanks :-)
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your nice replies, but my doubt is regarding the part of the complete statement that I have marked in bold face:


T is a class and a constructor for class T is invoked, or U is an array with element type T, and an array of type U is created.




Thanks,
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chandra can you please explain all the points which you have mentioned clearly once more,as am unable to understand what its saying.
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
am unable to understand the "rather" part in all cases.
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class or interface type T will be initialized at its first active use, which occurs if:
T is a class and a method actually declared in T (rather than inherited from a superclass) is invoked.
T is a class and a constructor for class T is invoked, or U is an array with element type T, and an array of type U is created.
A non-constant field declared in T (rather than inherited from a superclass or superinterface) is used or assigned.


1)(rather than inherited from a superclass)---this is for static inherited methods?am i right?
2)or U is an array with element type T, and an array of type U is created.
give me a example for this.please.

how are you scoring in enthuware mocks?when are you giving the final test?have you tried whiz..?chandra
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sharan vasandani:
am unable to understand the "rather" part in all cases.




Well,


A class or interface type T will be initialized at its first active use, which occurs if:
T is a class and a method actually declared in T Case #1 (rather than inherited from a superclass) is invoked.
T is a class and a constructor for class T is invoked, or U is an array with element type T, and an array of type U is created.
A non-constant field declared in T Case #2 (rather than inherited from a superclass or superinterface) is used or assigned.



See the example to get the things in better way:




Thanks,
[ May 07, 2007: Message edited by: Chandra Bhatt ]
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
am getting error with this Sub.class.getSuperClass(),
and

when i am calling Sub.class.getName() its working but
when i do Sub s=new Sub(); s.class.getName() it gives error.why so?

what is this class in between?

what about the array part in your first post which you had highlighted,can you give a example of that.
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS 3 is not mentioning any array:

from JLS 3:


from JLS 1:


This may be reason it doesn't work :-).
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, and here is the exact link:
http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chandra please help,i dont understand what john wants to say.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It is getSuperclass(); //small letter 'c'



what is this class in between?



"class" is literal. class literal works with regular classes as well as interfaces, arrays and primitive types, not reference variable.


when i do Sub s=new Sub(); s.class.getName() it gives error.why so?



With reference variable of a class you use getClass() method.


Thanks,
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


chandra please help,i dont understand what john wants to say.




Well Sharan, John is referring to JLS and I think I have give examples of each
case in my previous post regarding when the static block of a class will be executed and when not.

Let me summarize:

#1- When you instantiate that class.
#2- When you use any non constant fields of that class or any runtime constant of that class.

See the runtime constant:
class One {
final static int a;

static {
a=101;
}
}

Here a is runtime constant, so when you say One.a static block of that class
must be executed to assign value 101 to the a.

#3- When you call any method that is directly defined in that class instead of inheriting from the super class.

#4- You have an array and elements of the array are objects of that class.

#5- Any static field declared in that class is assigned.


Thanks,
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reffering to


I tried this, and couldn't get it work, so I check in JLS, and it seems, this has been removed from JLS 3.
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what about the assert statement which john has mentioned?
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> what about the assert statement which john has mentioned?
I believe this one is not reproducible. For details see resolution text of this bug:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6394986
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-----------------JLS---------------------
An assert statement causes the enclosing top level class (if it exists) to be initialized, if it has not already been initialized (�12.4.1).

Discussion

Note that an assertion that is enclosed by a top-level interface does not cause initialization. stmt312

Usually, the top level class enclosing an assertion will already be initialized. However, if the assertion is located within a static nested class, it may be that the initialization has nottaken place.
---------------------------------------

The following code is written to test assertion:
"An assert statement causes the enclosing top level class (if it exists) to be initialized".

In this test "assert" statement is located within a static nested class:

===============Code================
class Test01 {
static {
stmt31201.i = 1;
}

static class Test01_1 {
boolean test() {
boolean enabled = false;
assert enabled = true;
return enabled;
}
}
}

public class stmt31201 {
static int i = 0;

public static void main(String args[]) {
System.exit(run(args, System.out) + 95/*STATUS_TEMP*/);
}

public static int run(String argv[], PrintStream out) {
out.println("i = " + i);
Test01.Test01_1 obj = new Test01.Test01_1();
obj.test();
out.println("i = " + i);
if (i != 1) {
out.println("Top-level class hasn't been initialized");
return 2;
}
return 0;
}
}
===============Output============================
i = 0
i = 0
Top-level class hasn't been initialized
================================================

Hence the static initializer in Test_01 hasn't been executed.
It seems that realization contradicts the specification.
Posted Date : 2006-03-07 14:11:16.0


Work Around N/A

Evaluation This is an inconsistency between JLS and javac implementation.
Posted Date : 2006-03-16 07:06:55.0

There is no reason to believe that the specification should be changed. Asserts force initialization because that is the only way that static fields can be used within the assert. If the enclosing class has not been initialized, then the behaviour of the assert is misleading (at the very least) and undefined (at worst). If the provided example such that the assertion is:

assert stmt31201.i == 1;

Then this assertion will fail because the static initializer hasn't been run. This result is unexpected.

However, changing the behaviour could introduce compatibility problems. We need to understand the scope of the compatibilty problems to make a decision. It seems that the compatibility impact would be restricted to code which somewhat recklessly relies on the initializer not being run. Of course, this would only be relevant when assertions are enabled, which isn't usually the case for production code. If we require this initialization, then it is possible that if a bug exists due to an uninitialized class which was then initialized because assertions are enabled, the bug might cease to be reproducible.
Posted Date : 2006-08-09 20:00:42.0

1) Assertions in a static nested class which use static fields from an enclosing class, will always see the fields' initialized values. If Test01_1 said 'assert stmt31201.i == 1;' then stmt31201 will be initialized (if it wasn't already) to access its static member i.

2) Initialization doesn't usually care about enclosing classes. I presume that an assert statement forcing initialization of the top-level enclosing class was intended to give the JVM a chance to check whether assertions are enabled for the top-level class. If the initialization wasn't forced, a VM could legitimately say that no assertion is enabled in the enclosed class.

Dropping the requirement to initialize the top-level class would mean changing when the enabled/disabled status of assertions is checked. So I don't want to change the spec.

I suspect not many classes rely on initialization of another class NOT happening when the other class uses assertions and has an initializer.
Posted Date : 2006-11-30 23:40:59.0

-------------

chandra or anybody please explain what the person exactly wants to say.
what is the final conclusion.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sharan,

It seems contradictory. I think we should know all other conditions when
static initializer is executed besides the assert issue specifically for
exam. I don't find any way to produce this for now. If in near future, i
find any way to do that I will surely let you know.

I hope you get all other case well discussed above.

Thank you John Stone for your help to get the all the case well.


Thanks,
[ May 08, 2007: Message edited by: Chandra Bhatt ]
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
k thanks. ya i knw other cases well.
 
reply
    Bookmark Topic Watch Topic
  • New Topic