In the first code sample, a
is a main
method local variable. Method local variables need to be initialized before using them.
In the second code sample, a
is class member variable, hence it will be initialized to the default value.
More Related Contents:
- Initialization of an ArrayList in one line
- Variable might not have been initialized error
- What is Double Brace initialization in Java?
- Efficiency of Java “Double Brace Initialization”?
- How to initialize an array in Java?
- Static Initialization Blocks
- Difference between declaring variables before or in loop?
- In ArrayBlockingQueue, why copy final member field into local final variable?
- How can I initialise a static Map?
- What is an initialization block?
- When does static class initialization happen?
- Are fields initialized before constructor code is run in Java?
- Initializing multiple variables to the same value in Java
- Declaring and initializing variables within Java switches
- How to initialize an array of objects in Java
- Default constructors and inheritance in Java
- How to initialize HashSet values by construction?
- When is the static block of a class executed?
- java.lang.OutOfMemoryError: PermGen space [closed]
- Meaning of new Class(…){{…}} initialization idiom [duplicate]
- Cannot instantiate the type List [duplicate]
- Inner class and local variables
- Default value of ‘boolean’ and ‘Boolean’ in Java
- When does static initialization happen?
- Java Singleton Pattern
- Is the class loaded when its reference is declared?
- Java: how to initialize String[]?
- Initialize field before super constructor runs?
- Initialize final variable before constructor in Java
- Java : in what order are static final fields initialized?