variables?

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
.

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.

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

Joined: Jul 28, 2011
Posts: 9

posted Oct 27, 2011 07:01:48

0

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.

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)

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.

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).