1/26
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Static Variable
A variable that is shared among all instances of a class and has only one copy in memory.
Initialization of Static Variables
Static variables are initialized only once when the class is loaded into memory.
Change Reflection in Static Variables
If one object changes a static variable, that change is reflected for all other objects.
Usage of Static Variables
Commonly used for constants or counters, like 'Math.PI' or counting object instances.
Static Method
A method that can be called without creating an instance of the class.
Accessing Static Methods
Static methods are called using the class name and dot notation, e.g., 'ClassName.methodName()'.
Static Method Restrictions
Cannot access instance variables or instance methods directly because they lack a 'this' reference.
Memory Copies for Instance Members
Each instance member has one copy per object.
Memory Copies for Static Members
Static members have only one copy regardless of the number of objects created.
Class Scope Variables
Declared inside the class but outside any method, visible to all methods in the class.
Lifetime of Instance Variables
Exist as long as the object exists.
Local Scope Variables
Declared inside a method, constructor, or block, visible only within that block.
Lifetime of Local Variables
Created when the block is entered and destroyed when the block is exited.
Variable Shadowing
Occurs when a local variable has the same name as an instance variable, leading to the local variable taking precedence.
The 'this' Keyword
A reference to the current object upon which the current method or constructor is being called.
Resolving Ambiguity with 'this'
Used when a constructor parameter has the same name as an instance variable.
Passing Current Object Using 'this'
Allows a method to pass the current object to another method or object.
Static Accessing Non-Static
Mistake when trying to access instance variables in a static method without creating an object.
Local Variable Initialization Error
Occurs when trying to use a local variable without assigning it a value.
Variable Shadowing Logic Mistake
Assigning 'width = width;' does not initialize the instance variable correctly, requires 'this.width = width;'.
Misunderstanding Static Persistence
Static variables keep their value across the program unless explicitly changed.
Static Variable Example
'private static int nextId = 1000;' - a shared counter for all objects.
Indicating Static Members
The keyword 'static' is used to declare static variables and methods.
Class vs Instance Members
Instance members belong to specific objects, while static members belong to the class itself.
Accessing Instance Members
Instance members are accessed using the object name, e.g., 'objectName.member'.
Calling Static Methods Example
To call a static method, use 'ClassName.methodName()'.
Static Variable Effects
If a static variable is incremented by one object, all other objects see the updated value.