Variable Declarations
There are two types of variables in Java:
- Primitives: A primitive can be one of eight types: char, boolean, byte, short, int, long, double or float. Once a primitive has been declared, its primitive type can never change, although in most cases its value can change.
- Reference variables: A reference variable is used to refer to (or access) an object. A reference variable is declared to be of a specific type and that type can never be changed. A reference variable can be used to refer to any object of the declared type, or of a subtype of the declared type.
Declaring Primitives
- Primitive variables can be declared as class variables, instance variables, method parameters or local variables.
- We can declare one or more primitives, of the same primitive type, in a single line.
- All six number types in Java are made up of a certain number of 8-bit bytes, and are signed, meaning they can be negative or positive.
Type | Bits | Bytes | Minimum Range | Maximum Range | Default Value |
---|---|---|---|---|---|
byte | 8 | 1 | -27 | 27-1 | 0 |
short | 16 | 2 | -215 | 215-1 | 0 |
int | 32 | 4 | -231 | 231-1 | 0 |
long | 64 | 8 | -263 | 263-1 | 0 |
float | 32 | 4 | N/A | N/A | 0.0 |
double | 64 | 8 | N/A | N/A | 0.0 |
For boolean types there is not a range; a boolean can be only true or false. The size of boolean is virtual-machine dependent. The default value for boolean is false and for char is '\u0000'
Declaring Reference Variables
- Reference variables can be declared as static variables, instance variables, method parameters, or local variables.
- We can declare one or more reference variables, of the same type, in a single line.
Instance Variables
- Instance variables are defined inside the class, but outside of any method, and are only initialized when the class is instantiated.
- Instance variables are the fields that belong to each unique objects.
- Instance variables can use any of the four access levels.
- They can be marked final, transient and volatile.
- They cannot be marked abstract, synchronized, strictfp, and native.
- They also cannot be marked static, because then they'd become class variables.
Local Variables
- Local variables are variables declared within a method and need to be initialized with a value before can be used.
- The local variable is on the stack, if the variable is an object reference, the object itself will still be created on the heap.
- It is legal to declare a local variable with the same name as an instance variable, this is called "shadowing".
- There is never a case where an access modifier can be applied to a local variable.
- final is the only non-access modifier that can be applied to local variables.
The keyword this always refers to the object currently running.
Final Variables
- Declaring a variable with the final keyword makes it impossible to reinitialize that variable once it has been initialised with an explicit value.
- For primitives, this means that once the variable is assigned a value, the value can't be altered.
- A final reference still allows us to modify the state of the object it refers to, but we can't modify the reference variable to make it refer to a different object.
Transient Variables
- If we mark an instance variable as transient, we're telling the JVM to skip this variable when we attempt to serialize the object containing it.
- With serialization we can save an object to a file, or even ship it over a wire for deserializing at the other end, in another JVM.
Volatile Variables
- The volatile modifier tells the JVM that a thread accessing the variable must always merge its own private copy of the variable with the master copy in memory.
Static Variables and Methods
- Variables and methods marked static belong to the class, rather than to any particular instance.
- In fact, we can use a static method or variable without having any instances of that class at all.
- We need only have the class available to be able to invoke a static method or access a static variable.
- But if there are instances, a static variable of a class will be shared by all instances of that class; there is only one copy.
- A static method can't access a non-static (instance) variable, because there is no instance!
- The same applies to instance methods; a static method can't directly invoke a non-static method.
- The Java language also allows us to use an object reference variable to access a static member.
- Static methods can't be overridden!
- The following things can be marked as static:
- Methods
- Variables
- A class nested within another class, but not within a method.
- Initialization blocks
- The following things we can't mark as static:
- Constructors
- Classes (unless they are nested)
- Interfaces
- Inner class methods and variables.
Comments
Post a Comment