• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

default initialization

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {
public static void main ( String[] args) {
int value;
value = value + 1;
System.out.println(" The value is : " + value);
}
}
Here compiler is saying not have initialize the value of "value".
But what I know is that compiler default initialize it to its type value as it is integer so it would be 0.
Please comment
thanks in advance
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Payal,
Default initialization takes place only for member variables (static or instance).
Your "value" is a local variable. Local variables has to be initialized by the programer.
The only default initialization in local scope I know is that Array e l e m e n t s (not the array itself) are initialized to default values (0, false or null) in local scope (after initializing the array (example: String ar[] = ar[2]), too.
correct me if I am wrong
Axel
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The variables which intializes by default are declare in class.That is either as a class variables or instance variables.In your case you have declare value in main method.
Regards,
Hassan
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi payal,
local variables, those declared in methods or blocks of code must be definitely assigned a value before you attempt to use them. As others have pointed out, they are not set to any default value. (See the JLS Chapter 16).
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic