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.
What is a static variable?
A variable shared by all instances of a class; belongs to the class itself.
What is a static method?
A method that belongs to the class rather than any specific object of the class.
What does the static keyword indicate?
That a variable or method belongs to the class itself, not an instance of the class.
What is an instance variable?
A variable that is unique to each instance (object) of a class.
What is the dot operator used for with static members?
Accessing static variables and methods using the class name.
What is the purpose of final keyword?
The final keyword is used to declare a constant variable whose value cannot be changed after it is initialized.
What is a class variable?
Another name for a static variable, emphasizing that it belongs to the class.
What is scope in programming?
The region of a program where a defined variable can exist and be recognized.
Define 'getter' method.
A method used to retrieve the value of a private variable.
Define 'setter' method.
A method used to set the value of a private variable.
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