Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

variables?

 
shiva prasad.
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java how variables are initialized by jvm?
Ex:
public class Test{
int i = 5;
int j;
public void tester(){
i = 10;
j =5;


}
}

How the variables are initialized during the above situations?
1.Object is just created
2.While executing the method "tester".
Don't tell me that these are primitives and are directly initialized to default values first and then assigned values, Please let me know the flow of jvm
.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shiva prasad. wrote: . . . these are primitives and are directly initialized to default values first and then assigned values, . . .
They are initialised to default values and then assigned values.

If you want to know the flow of execution, put lots of System.out.println() instructions in your code. Print out the line numbers and the values of the variables. Or use a pencil and paper, but don’t expect us to do your work for you.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't tell me that these are primitives and are directly initialized to default values
Not all primitives are given a default value - or rather it depends on the variable and not type to get a default value.
 
shiva prasad.
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:
Don't tell me that these are primitives and are directly initialized to default values
Not all primitives are given a default value - or rather it depends on the variable and not type to get a default value.


Hi Jhon Jai , I want an example to understand the point you have mentioned.
As far as my knowledge the jvm first reads the statements then assigns default values to the variables declared,
then again jvm checks for the variables which are initialized the during declaration/initialization and assigns them with initialized values.

Am I correct let me know,please explain with an example.
And thanks for answering my qusetion.

Hi Campbell Ritchie thanks for your suggestion.
Let me know can I put a sysout statemnt which can print the values of variables while the jvm reads code statements before creation of object?
Of course I can debug my code and achieve that.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shiva prasad. wrote:
Hi Jhon Jai , I want an example to understand the point you have mentioned.
As far as my knowledge the jvm first reads the statements then assigns default values to the variables declared,
then again jvm checks for the variables which are initialized the during declaration/initialization and assigns them with initialized values.

Local variables declared in the method are not initialized with default values.

shiva prasad. wrote:
Hi Campbell Ritchie thanks for your suggestion.
Let me know can I put a sysout statemnt which can print the values of variables while the jvm reads code statements before creation of object?
Of course I can debug my code and achieve that.

You can use System.out.println (Used to print to the standard output) to print the variable contents only after it has been declared (and initialized if they are local variables)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shiva prasad. wrote: . . . Let me know can I put a sysout statemnt which can print the values of variables while the jvm reads code statements before creation of object? . . .
No. Those are instance variables, and they do not exist before you create your object. You can try putting your print statements as the first line of the constructor, however, or in an instance initialiser.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shiva prasad. wrote:
Don't tell me that these are primitives and are directly initialized to default values first and then assigned values, Please let me know the flow of jvm
.


You should read about the constructors and instance initializers (as what Campbell mentioned) to get to know the flow. However, you can get to know the FLOW only by putting a SOP (System.out.println) statements before and after each statement. Or you can use a debug facility in your IDE (Eclipse, Netbeans whatever you use).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic