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
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.
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.