Topic 1.1: Java Basics - Define the scope of variables Flashcards
By default in java the variable with the closest scope takes precedence and hides access to the variable with the same name but in a longer scope.
how is
shadowing
in variables resolved by java
what is the place of declaration and the place of initialisation of
local variables
Place of declaration: Can be defined in a constructor, method, or code block.
Place of initialization: Local variables should be initialized at the point of declaration. (they will not be given default values if not explicitly initialised)
Place of declaration: Can be defined in a constructor, method, or code block.
Place of initialization: Local variables should be initialized at the point of declaration. (they will not be given default values if not explicitly initialised)
what is the place of declaration and the place of initialisation of
local variables
what is the place of declaration and the place of initialisation of
Instance Variables (Fields)
Place of declaration: Defined within the class but outside constructors and methods.
Place of initialization: Typically initialized within the constructor.
what 2 ways can
shadowing
in variables be resolved by the programmer
to resolve this the programmer may:
* change the name of the variables with a shared name
* use the this
keyword to explicitly access the instance variables
name 3 situations of
Shadowing
in Variables
Three situations of this occuring are:
* A parameter shadows a field.
* A local variable shadows a field.
* A local variable shadows a parameter.
Scope: Throughout the class, accessible from any constructor, method, or code block.
Lifetime: As long as the program is running.
what is the scope and lifetime of
Class Variables (Static Variables)
Place of declaration: Defined within the class but outside constructors and methods.
Place of initialization: Can be initialized at declaration or using a static block.
what is the place of declaration and the place of initialisation of
Class Variables (Static Variables)
Place of declaration: Defined within the class but outside constructors and methods.
Place of initialization: Typically initialized within the constructor.
what is the place of declaration and the place of initialisation of
Instance Variables (Fields)
how is
shadowing
in variables resolved by java
By default in java the variable with the closest scope takes precedence and hides access to the variable with the same name but in a longer scope.
to resolve this the programmer may:
* change the name of the variables with a shared name
* use the this
keyword to explicitly access the instance variables
what 2 ways can
shadowing
in variables be resolved by the programmer
what is the place of declaration and the place of initialisation of
Class Variables (Static Variables)
Place of declaration: Defined within the class but outside constructors and methods.
Place of initialization: Can be initialized at declaration or using a static block.
define
Shadowing
in Variables
This occurs when two separate variables share the same name and are both in scope
Place of declaration: Declared within the header of the constructor or method.
Place of initialization: Initialized when the constructor or method is invoked.
what is the place of declaration and the place of initialisation of
Constructor and Method Parameters (Formal Parameters)
what is the scope and lifetime of
Constructor and Method Parameters (Formal Parameters)
Scope: Limited to the constructor or method block they are declared in.
Lifetime: Exist as long as the constructor or method is executing.