All Flashcards
Why use static variables?
To store values that are common to all instances of a class, like constants or counters.
Why use static methods?
To perform operations that don't depend on the state of a specific object.
Can static methods access instance variables?
No, static methods cannot directly access or modify instance variables.
How do you call a static method?
Using the class name followed by the dot operator and the method name (e.g., ClassName.staticMethod()).
What is the relationship between objects and static variables?
All objects of a class share the same static variable; changing it in one object changes it for all.
What is the 'this' reference?
A reference to the current object; static methods do not have a 'this' reference.
What is the significance of static final variables?
They represent constants that are shared across all instances of the class and cannot be changed.
How does using static members improve memory usage?
Static variables are stored only once per class, not once per object, saving memory.
Explain the concept of class-level scope for static variables.
Static variables have class-level scope, meaning they are accessible throughout the class, regardless of instance.
What is the difference between static and non-static methods?
Static methods belong to the class, non-static methods belong to the instance.
Give an example of a real-world use case for static variables.
Counting the number of instances of a class created.
Give an example of a real-world use case for static methods.
Utility methods in a Math class, like calculating square roots or trigonometric functions.
How can static variables be used in game development?
To store game-wide settings or high scores.
How are static methods used in the Singleton design pattern?
A static method provides global access to the single instance of the class.
How are static variables used to manage resources?
They can track the number of active connections to a database.
Describe a scenario where a static method is used for data validation.
A static method can validate input data format without needing an object instance.
How are static variables used in configuration management?
To store default configuration settings that apply to all instances.
How are static methods used for logging?
A static method can write log messages to a file without needing an object instance.
How are static variables used to store application version information?
A static variable can store the version number of the application.
How are static methods used in unit testing?
Static methods can be used to set up test environments or perform utility functions.
What is the output of the following code?
java
Student.setSchool("New School");
System.out.println(Student.school);
"New School"
Identify the error in the following code:
java
public class Example {
public int x = 5;
public static void printX() {
System.out.println(x);
}
}
Non-static field 'x' cannot be referenced from a static context.
What is wrong with the following code?
java
public class MyClass {
static int count = 0;
public MyClass() {
count++;
}
public static void main(String[] args) {
MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();
System.out.println(obj1.count);
}
}
Accessing a static variable through an object (obj1.count) is legal but misleading; it should be accessed via the class name (MyClass.count).
What is the output of the following code?
java
public class Test {
static int x = 10;
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = new Test();
t1.x = 20;
System.out.println(t2.x);
}
}
20
What is the output of the following code?
java
public class Example {
static int counter = 0;
Example() {
counter++;
}
public static void main(String[] args) {
Example e1 = new Example();
Example e2 = new Example();
System.out.println(Example.counter);
}
}
2
Identify the error in the following code:
java
public class StaticTest {
private int numInstances = 0;
public static void increment() {
numInstances++;
}
}
Cannot make a static reference to the non-static field numInstances.
What is the output of the following code?
java
public class StaticExample {
static String message = "Hello";
public static void main(String[] args) {
StaticExample.message = "Goodbye";
System.out.println(StaticExample.message);
}
}
Goodbye
What is the output of the following code?
java
public class StaticCounter {
static int count = 0;
StaticCounter() {
count++;
}
public static void main(String[] args) {
StaticCounter obj1 = new StaticCounter();
StaticCounter obj2 = new StaticCounter();
System.out.println(StaticCounter.count);
}
}
2
What is the output of the following code?
java
public class StaticMethodTest {
int x = 5;
static int y = 10;
static void print() {
System.out.println(y);
}
public static void main(String[] args) {
print();
}
}
10
What is the output of the following code?
java
public class StaticVariableTest {
static int count = 0;
public static void main(String[] args) {
StaticVariableTest.count = 5;
System.out.println(StaticVariableTest.count);
}
}
5