• 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

Local variables

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as i know, that local variables need to be explicitly
initialized, until i tried out the foll. example :
1.class test
2.{
3.public static void main(String args[])
4.{
5. int a = 12,b;
6. System.out.println("Hello");
7.
8. int i = new test().aName();
9. System.out.println( i );
10.}
11. public int aName()
12. {
13. int y=9,x;
14. return y;
15. }
16.}
At line 13, i am not initializing variable x, why is it allowed ?
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you don't use it. The rule is that a variable has to be initialized before it is used. You don't have to set the value at declaration.
The following peace of code is allowed (read this as a part of a program, b is declared somewhere else)

ok?
/Mike
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mikael Jonasson:
[B]Because you don't use it. The rule is that a variable has to be initialized before it is used. You don't have to set the value at declaration.
The following peace of code is allowed (read this as a part of a program, b is declared somewhere else)

ok?
/Mike[/B]



Yeah, got it.
thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic