Search code examples
androidoncreate

Attribute initialized in onCreate()


I created my class (exending Activity) with an attribute int a.
The attribute is automatically initialized to 0 in the method onCreate().

Is it normal?


Solution

  • This is normal. You see, "int" is a primitive type. It is not an object, and so, it cannot hold a "null" value. If you want your variable to be null at onCreate(), you must change its type to the Object representation of it. The "Integer" class represents the primitive type "int".