Static in Java
Static in Java
How many static variables, local variables and instance variables are present in the
code given below?
public class Car {
private String vehicleNumber;
private String color;
private static int numberOfCars = 0;
Options :-
Person() {
age = 20; // line 2
}
class Computer {
private static int id;
private static int counter = 0;
public Computer() {
id = ++counter;
}
123
333
012
000
Q5
Predict the output of the code given below.
class Printer {
static {
System.out.println("Static block in Printer class");
}
static {
System.out.println("Static block in Tester class");
}
Static block in Printer class; Static block in Tester class; In main; The value of
sample variable is: 2
Static block in Tester class; Static block in Printer class; In main; The value of
sample variable is: 2
Static block in Tester class; In main; Static block in Printer class; The value of
sample variable is: 2
Static block in Tester class; In main; The value of sample variable is: 2; Static
block in Printer class
Q6
What will be the output of the below code ?
public class Tester {
public int var;
Tester(int var) {
System.out.println(this.var);
}
20 20
00
compilation error: cannot use 'this' inside main
20 0