• 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

var initialization

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is this giving a compiler error?
class A {
int i;
i = 9; //error here
public static void main(String[] args) {}
}
Is it necessary that I should
either initialize at the time of declaration
or just declare it without explicit initialization (a member var will be automatically initialized)
or initialize inside a method/constructor ?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jini
The only thing you can have in a class and outside a method is static blocks. Thus the initialization will give complie time error.
Hope that helps.
Rekha
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting...
You can assign the value at the time you declare it (int i = 0) to avoid a compile error.
Alternatively, you can assign the value in the constructor, or within a method.
Thanks for bringing this up. I'd run into the same problem....
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jini,
If i initialize i during declaration, it'll comply and run fine.
class A {
int i=9;
public static void main(String[] args) {}
}
Could someone explain further why the above one is ok while the one posted by Jini.
 
Jini Varghese
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. It helped....
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kenneth Ho:
Hi Jini,
If i initialize i during declaration, it'll comply and run fine.
class A {
int i=9;
public static void main(String[] args) {}
}
Could someone explain further why the above one is ok while the one posted by Jini.


Think of int i; as int i=null;. Therefore your second line i=9; would be an assignment and not an initialization, whereas int i=9; is an initialization.
hope it helps.
 
Jini Varghese
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's nice...
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic