Walter Ayven
Walter Ayven

Reputation: 1

Java variable value initialisation

I am a beginner in Java, struggling to understand the following problem with variable initialisation, would appreciate an expert help.

Given the code from an exam:

public class SimpleCalc {
    public int value;
    public void calculate() { value += 7; }
}

AND

public class MultiCalc extends SimpleCalc {
    public void calculate() { value -= 3; }
    public void calculate(int multiplier) {
        calculate();
        super.calculate();
        value *= multiplier;
    }
    public static void main (String[] args) {
        MultiCalc calculator = new MultiCalc ();
        calculator.calculate(2);
        System.out.println(calculator.value);
    }
}

My understanding is that this needs to throw a runtime exception since variable "value" never gets an actual preliminary value assigned to it (public int value;). But, the code works and behaves as if the variable "value" is assigned 0 (same as public int value=0;). Could someone explain please why does this happen? Many thanks

Upvotes: 0

Views: 895

Answers (4)

Peter Lawrey
Peter Lawrey

Reputation: 533880

My understanding is that this needs to throw a runtime exception since variable "value" never gets an actual preliminary value assigned to it (public int value;).

Failing to initialise a variable which must be initialised is checked by the compiler and is a compile time error.

Runtime errors are thrown at Runtime. i.e. when you run a program which has been successfully compiled. The only runtime error you will get from failing to set a value is a NullPointerException (for accessing a null reference), or rarely an ArithmeticException if you divide by an integer which is zero.

final fields and local variables must be initialised, but non final fields will value their default value.

Upvotes: 1

Anuj Balan
Anuj Balan

Reputation: 7729

This kind of initializations happen for instance variables, in java. The variable 'value' is an instance variable which exists in each of the object of that class. Instance variables of data type int have default value 0 assigned to it. Different data types(of instance variables) have different default values.

Example

Upvotes: 0

maloney
maloney

Reputation: 1663

Primitive types in Java always get assigned a default value. For an int this is always 0.

If you were to use a Integer object (rather than int) you would see a null pointer exception being thrown if it wasn't initialised.

Upvotes: 0

PermGenError
PermGenError

Reputation: 46438

. But, the code works and behaves as if the variable "value" is assigned 0 (same as public int value=0;).

instance variables in java get default values. i.e, int get 0 as default value, float get 0.0 and so on. thus if you don't initialize, they get default values.

Data Type   Default Value (for fields)
byte    0
short   0
int 0
long    0L
float   0.0f
double  0.0d
char    '\u0000'
String (or any object)      null
boolean false

Upvotes: 8

Related Questions