Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

initialization sequence

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't predict the sequence of execution in the following code
class test
{
private int i=mym();
private int j=9;

private int mym(){
return j;
}
public static void main(String args[]){
System.out.println((new test()).i);
}
};
the ans is 0 displayed on the screen
help me on this pls?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ameen ahamed
It's really a good question.
Your code I just added the Line numbers(I am explaining through the line number)
1 : class test{
2 : private int i=mym();
3 : private int j=9;
4 : int mym(){
5 : return j;}
6 : public static void main(String args[]){
7 :System.out.println((new test()).i);
8 :}
9 :};

At the run time only the values are going to allocate in the memory. Before that only empty memory is there. When the control enter in the main method i and j are zero (Because of int).
In main method you are making the object and trying to access the value of i in line number 7.
The control is going to the following way.
1) Line number 7
2) Line number 2
3) Line number 4
4) Line number 7
The run time environment is not entered line number 3. Before that you deveate the control to other line.
Try and understand:
Just interchange the lines of 2 & 3 you will get the prober output. Try to print inside the mym also. There also it give zero only.
I think now your problem is clear.

If I am wrong, you can mail me.
My mail id is :sanjaykumaresan@rediffmail.com
Bye
M.Sanjay

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by sanjay kumaresan:
The control is going to the following way.
1) Line number 7
2) Line number 2
3) Line number 4
4) Line number 7
The run time environment is not entered line number 3. Before that you deveate the control to other line.


Sanjay, basically, this question relates to instanse initialization - the instanse of the class should be initialized before using it so the statement "The run time environment is not entered line number 3" is false - in your terms, you missed "line 4) Line number 3" so the correct order is
1) Line number 7
2) Line number 2
3) Line number 4
4) Line number 3
5) Line number 7
Of course this doesn't affect the result
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ameen,
When the JVM creates a new instance it

  • allocates memory for all the declared fields
  • sets all fields to the default values of their delcared types (except for static final fields initialized with literals, these never are set to their default values)
  • initializes static fields and runs static initialization blocls in the order they appear in the file
  • initializes instance field and runs instance initialization blocks in the order they appear in the file

  • Your example has two instance fields: 'i' and 'j'. These are both set to the default value for 'int' types: 0
    Next 'i' is initialized. It references the method 'mym()' which returns the value of 'j' which at this point equals '0'
    For more information on the above works see JLS �8.3.2
    Hope that helps.

    ------------------
    Jane Griscti
    Sun Certified Programmer for the Java� 2 Platform
    [This message has been edited by Jane Griscti (edited July 01, 2001).]
 
Andy Skalkin
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jane Griscti:
Hi ameen,
When the JVM creates a new instance it
...
initializes static fields and runs static initialization blocls [b]in the order they appear in the file

...
[/B]


Jane, as far as I know JVM initializes static fields only once - the first time this class is loaded into JVM.
 
ameen ahamed
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class test
{
private int i=j;
private int j=9;
private int mym(){
return j;
}
public static void main(String args[]){
System.out.println((new test()).i);
}
};
why the above code is not compiled. It is giving compile time error? why?

Originally posted by Jane Griscti:
Hi ameen,
When the JVM creates a new instance it


  • allocates memory for all the declared fields
  • sets all fields to the default values of their delcared types (except for static final fields initialized with literals, these never are set to their default values)
  • initializes static fields and runs static initialization blocls [b]in the order they appear in the file
  • initializes instance field and runs instance initialization blocks in the order they appear in the file

  • Your example has two instance fields: 'i' and 'j'. These are both set to the default value for 'int' types: 0
    Next 'i' is initialized. It references the method 'mym()' which returns the value of 'j' which at this point equals '0'
    For more information on the above works see JLS �8.3.2
    Hope that helps.
    [/B]


 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you tried to assign j to i when j was not declared yet.
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your question, i was 0 instead of 9 because when i was assigned its value, JVM has not called the constructor yet, and neither was j assigned 9. Hence, i stayed 0 instead 9.
[This message has been edited by Cameron Park (edited July 02, 2001).]
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andy,
You're right; slip of the tongue .. fingers If the class has already been loaded the static fields will not be re-initalized when an instance is created.
Thanks for catching my mistake.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform

[This message has been edited by Jane Griscti (edited July 03, 2001).]
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!