aspose file tools*
The moose likes Java in General and the fly likes variables? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "variables?" Watch "variables?" New topic
Author

variables?

shiva prasad.
Greenhorn

Joined: Jul 28, 2011
Posts: 9
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

Joined: Oct 13, 2005
Posts: 40027
    
  28
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
Bartender

Joined: May 31, 2011
Posts: 1776
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
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

Joined: Sep 08, 2007
Posts: 3096
    
  33

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)


Mohamed Sanaulla | My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
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

Joined: Apr 20, 2006
Posts: 3355

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


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variables?