Declaration is the process of defining the variable, along with its type and name
Initialization, on the other hand, is all about assigning a value
Java primitive types; variables of this type hold their values directly.
Reference types hold references to objects (instances of classes).
Unlike primitive types that hold their values in the memory where the variable is allocated, references don’t hold the value of the object they refer to.
a reference points to an object by storing the memory address where the object is located
Java doesn’t allow us to discover what the physical memory address is
objects creation is a bit more complex
we trigger the initialization using the new keyword
invokes a constructor and initializes the object in memory
new keyword is responsible for allocating memory for the new object through a constructor
the compiler will create a default constructor which has no arguments,
As soon as we declare these variables, they’re given a default value:
Instance and class variables don’t require us to initialize them
Local variables must be initialized before use, as they don’t have a default value and the compiler won’t let us use an uninitialized value
The final keyword applied to a field means that the field’s value can no longer be changed after initialization
Constants must be initialized either when they’re declared or in a constructor
initializer is a block of code that has no associated name or data type and is placed outside of any method, constructor, or another block of code.
Java offers two types of initializers, static and instance initializers
the order for initialization statements is as follows: static variables and static initializers in order instance variables and instance initializers in order constructors
Glasp is a social web highlighter that people can highlight and organize quotes and thoughts from the web, and access other like-minded people’s learning.