• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Output or Compilation error? Surprising!

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers!!
See the following program and tell me what is the output?

The options are:
A. 100
B. 10
C. 0
D. 1
E. Compiler error, undefined variable j
F. Compiler error, j is not yet initialized.

Whoa it is C. 0
The point behind this is that every static variable initialized to it's respective default value at start of the program. So in the program when access "z.i" which is in front of the intializing of "j" , and called "peek()" that return j, which at that point not intialized with 100 but by default it is intialized to 0. So the output is 0.
Regards
Farhan
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is my understanding is when initialising the static int i it invokes peek() method, at that time it finds a variable j which has to be returned, but it is not defined yet, so it temporarily creates a variable j and initialises it to 0 and returns it. Only where it is not explicitly initialised it is initialised to the default values, not in all the cases.
In this case static int j has been initialised to 100. So if it finds variable j at the time of peek() method operation it definately takes its initialised value of 100, but not happens in this case because it has not defined yet, defined later.
Variable i will not get initialised to 100 till you define the variable j before the peek() method and variable i after the peek() method in the code. Even if you create an object of class Z and invoke the i variable of that object till you will get 0 only, not 100. (I have tried this).
Please correct me if I am wrong.
Regards
V. Srinivasan
[This message has been edited by V Srinivasan (edited June 07, 2001).]
 
Farhan Tariq
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Srinivasan!


What is my understanding is when initialising the static int i it invokes peek() method, at that time it finds a variable j which has to be returned, but it is not defined yet, so it temporarily creates a variable j and initialises it to 0 and returns it. Only where it is not explicitly initialised it is initialised to the default values, not in all the cases.


I don't admit the above quote from your answer because when you define some variable with static the case is then:
* At loading time of class all static variables are initialized to their respective values. And have memory location set.
* Then comes execution part as user defined intialization takes place.
I think that will clear u
Tell me if i'm wrong or miss something
Regards
Farhan
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends
As you know, at the first time that one of the static members (variable or method) of class is accessed, memory will be allocated for them and instantly (before assigning initial values), they will be set to zero. That is all of the object references will be null and all of the primitive values will be equal to 0 (integer), or 0.0 (float and double)...
Refer to Thinking in Java, the chapter that explains initilization.
 
V Srinivasan
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Friends, I got now, it allocates memory and initialises to the default value first and then assigns value if any in later stage.
Only for final variable it assigns value while allocating memory.
Thanks and regards,
V. Srinivasan
[This message has been edited by V Srinivasan (edited June 07, 2001).]
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
if I comment out the initialization of the class variable j I get a strange result (for me at least).
class Z {
static int peek() { return j; }
static int i = peek();
// static int j = 100; Commented out !!!
}
public class Test {
public static void main(String[] args{
System.out.println(Z.i);
}
}
result: 0
Shouldn't there be compiler error? Because after commenting the indicated line out, j is a variable of method scope. And in methods variables have to be initialized. Sure?
please help
Axel
[This message has been edited by Axel Janssen (edited June 07, 2001).]
[This message has been edited by Axel Janssen (edited June 07, 2001).]
 
V Srinivasan
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Axel,
Unless you define a variable j within the scope of method it will give compile time error as Undefined variable j.
Regards,
V.Srinivasn
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srinivasan,
this was my error. Did not save the file before compilation. Sorry for that.
The initial question of Farhan remains open.
What does happen, when the class is initialized with the class member access (Z.i)???
I don�t have any idea. Even if I put a second System.out.println(Z.i) the value remains zero.
If I use final modifier for Z.i it gives 100.
Any ideas???
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Axel,
As I see it when i is initialized it is initialized with a value of 0. Even though j is set to 100 later it doesn't cause the peek() method to be called for i. I believe an interesting situation would be if we had i, j and peek() defined as objects of some classes. If we print Z.i it would give us null even if we assign a reference to j.
I couldn't understand how you got a value of 100 with final modifier. I would appreciate if you could share with us that piece of code.
regards

Originally posted by Axel Janssen:

The initial question of Farhan remains open.
What does happen, when the class is initialized with the class member access (Z.i)???
I don�t have any idea. Even if I put a second System.out.println(Z.i) the value remains zero.
If I use final modifier for Z.i it gives 100.
Any ideas???


 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
This is a strange little program When 'Z.i' is referenced the entire class 'Z' is initialized. I always thought 'static' elements are initialized in the order in which they appear. However, if you move the <code>peek()</code> method so it follows the 'i' and 'j' initializations, the code will still compile correctly and return '0'.
In this example, <code>int i = peek()</code> the peek() method is executed when 'Z.i' is referenced. If you add a <code>System.out.println</code> statement to the method you'll see it does get executed. However, at that point in the code, <code>j</code> has not yet been fully initialized; so 'i' = 0. If you move <code>static int j = 100</code> ahead of the <code>int i</code> statement then '100' will be returned.
Or, if you leave the <code>static int j = 100</code> statement where it is but add the keyword final then 'i' will equal '100'.
What's all these show? Static variables are allocated memory and set to their default values, static methods are initialized and then static fields receive their assigned values in the order in which they appear in the code. The exception is <code>static final</code> fields declared with a literal value. They are initialized to the literal value vs the default.

Out of curiousity, I created 'j' as a blank final and then added a static initializer block to set it.

The code compiles, with the same output 'i' = 0; so static initializers and static fields are executed based on the order in which they appear.
Interesting question Farhan .. I learn something new every day
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Farhan Tariq
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!
Thanx Jane!. I think more points cleared to me that:

For the member variable declaration & initialization in case of static:
---------------------------------------------------------------

At loading point all the static member variables intialized to their default respective value but if they are final "too" then at loading point they get their value too.
For the member variable declaration & intialization in case of non-static:
---------------------------------------------------------------

At execution point when an instance is created then all non-static member variables initialized to their default respective values (so in forward referencing they will not show their assigned value but default value). But in case of final here they intialized to their given value at the point when instance was created.

But we have one more point here that if we don't use final in case of static or non-static, it will give us always the default value in case of forward referencing. Which means in final case ,I think, member variable got some type of reference to that variable or save the value for that variable because it is final.
"tell me if i miss something"
Regards
Farhan
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
First thing in office was to check this thread. Really interesting. Thanks Jane for clarify. I admit to your first-allocated-to-default-value-and-then-order-of-code theory.
Farhan: As far as I know final variables get the value at compilation time not at loading point.
Anshul: I just put final in the initialization of the j variable in class Z. So the line in Farhans example (first entry in discussion - thread) is:
static final int j = 100;

have a nice day
Axel


[This message has been edited by Axel Janssen (edited June 08, 2001).]
 
Farhan Tariq
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Axel!
You should or must have this concept in your mind that "No memory allocation occurs at compilation time".
So final variables r also get memory allocation at loading time but the difference they make is before the non-final variables.
I think the flow is like this:
Memory Allocation
-----------------------------
1. final static variables
2. static variables
3. final non-static variables
4. non-static variables
-----------------------------
Point is that all get memory allocation after we type "java <filename>"
first two get memory allocation at loading time and last two will get memory allocation at execution time.
Hope this make the point clear to you
Farhan
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Confess that still I am not very smart in this stuff. So I guess Farhan is right.
There is still an unclear point for me.
I allways thought that classes are loaded ONCE. If this would be the case a second call to Z.i should give 100, because the variable had been assigned to that value.
But in the example a second call to Z.i results in 0
Result of below code:
0
0
<code>
class Z {
static int peek() { return j; }
static int i = peek();
static int j = 100;}
public class Test {
public static void main(String[] args) {
System.out.println(Z.i);
System.out.println(Z.i);
}
}
</code>

apreciate your help
Axel

 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Farhan,
I did not think in memory allocation. I thought that final would result in something like a replacement of the variable through the value at compilation time.
But it may be nonsense.
Axel
 
Farhan Tariq
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Axel again(haa haa)!!
There is no question about compilation because compilation only check the syntax and possibility not the "result".
Your question is very simple because it relates to "Forward Referencing". Variables whether they r static or non-static get user given value at their execution time. So j is intialized but still not get the execution of j=100;. So every time you attemt to do so, value 0 will be printed. Because at peek() JVM don't know what is the value assigned to j which comes later.
Regards
Farhan
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
Interesting question and it has cleared my doubt about state of static variables when class is loaded first, thanks everybody.
Jyotsna
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gr8 quesion.......

What I think here is that it has nothing to do with memory allocation of final variables before normal variables.


As static variables have to be initialized at load time. All variables are first initialized to their default values and then the initialization takes place acc. to the sequence of declaration. However, in case of final variables initialization is done with the value supplied rather than default value. As it can only be initialized once.

Adeesh
 
reply
    Bookmark Topic Watch Topic
  • New Topic