Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default values

 
Anvi Dixit
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,

Why is that only local variables are not assigned default values and instance variables are?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic