This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Default values Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Default values " Watch "Default values " New topic
Author

Default values

Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45
Hello

please find below the code:


now here i didnt intialize the variable keeping in my that the default value fo the int variable is 0. But i am getting an error (a red line) at line no 5 .
Please help me out though it is a very basic question.
Thanks
Anvi
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Do all primitive variables get default values? Read about it a bit and I am sure it would go a long way in helping you about default value assignment.
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
Local variables are not assigned default values, instance variables are.

Therefore having an int defined at that scope would not get the default value of 0 either. It would however if you'd put it outside of your method as an instance variable.

I'd recommend reading the compiler error, as it will say something like variable i may not have been initialised.
Asha Lilliete
Greenhorn

Joined: Jun 28, 2006
Posts: 6
All the local variables must be initialized.
where as a member variable neednt be, as it is initialiaed automatically.
If you initialize the byte b and compile it ,
the error should not occur.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 8795

Ok,

Why is that only local variables are not assigned default values and instance variables are?


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Default values
 
Similar Threads
Problem with "private static"
arithmetic promotion
Code output -Doubt.
Casting Primitives
Why does'nt it get a default value..?