• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

initialization value for member varibles

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading "The complete Java 2 Certification study guide" by Philip and Simon. On page 17, it writes "All member varibles that are not explicitly assigned a value upon declaration are automatically assigned an initial value", it also give a table about the initialization values for member variables. But when I do a simple test code as following:
public class Test {
public static void main(String argv[]){
int i;
System.out.println(i);
}
}
I got the error message:
variable i might not have been initialized
System.out.println(i);
^
So, anyone can help me on that?
Thanks.
Holly
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because i is not a member variable, Holly. To compile, it should've been declared outside any method in your class. Otherwise, you absolutely have to assign it an initial value.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a variable is declared inside a method, it is known as "local variable". Local variables does not get a default value, it has to be explicitly initialized before the first usage.
Unni
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic