Java local variable vs class field initialization and default values

This is a common error when starting out with Java: error: variable example might not have been initialized

In Java, Class instance properties are initialized to default values if not explicitly defined with an initial value. For local variables however (e.g. variables local within method or a block scope), default values are not automatically assigned, meaning that you must explicitly initialize them otherwise you’ll see the compile error above.

This is described in the Java tutorial here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.