0% found this document useful (0 votes)
15 views4 pages

Java Static and Instance Variables Explained

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Java Static and Instance Variables Explained

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q1

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;

public Car(String vehicleNumber, String color) {


[Link] = vehicleNumber;
[Link] = color;
}

public void display(String name) {


[Link]("Owner's name: " + name);
[Link]("Vehicle number: " + vehicleNumber);
[Link]("Color: " + color);
}
}

Options :-

static variables: 1; instance variables: 2; local variables: 1


static variables: 3; instance variables: 0; local variables: 2
static variables: 1; instance variables: 2; local variables: 3
static variables: 3; instance variables: 2; local variables: 3

Q2 Which of the following is/are correct?

1. Static members can only be accessed by static methods.


2. Non-static members can be accessed by static methods.
3. Static blocks get executed only once.
4. Static variables and methods can be accessed without creating an object of the
class.

Q3 Choose the correct option based on the code given below.


class Person {
private String name;
private static int age;
static {
name = "john"; // line 1
}

Person() {
age = 20; // line 2
}

public int getAge() {


return age; // line 3
}
}

public class Account {


public static void main(String[] args) {
[Link](new Person().getAge());
}
}

1) Compilation error in Line 1 as a non-static member cannot be accessed inside a


static block.

2) Compilation error in Line 2 as a static member cannot be accessed inside a


constructor.

3) Compilation error in Line 3 as age is a static variable and cannot be accessed


inside a non-static method.

4) Code executes successfully and displays 20.

Q4 What will be the output of the code given below?

class Computer {
private static int id;
private static int counter = 0;

public Computer() {
id = ++counter;
}

public int getId() {


return id;
}

// Rest of the methods


}

public class Tester {


public static void main(String[] args) {
Computer comp1 = new Computer();
Computer comp2 = new Computer();
Computer comp3 = new Computer();
[Link]([Link]() + " " + [Link]() + " "
+ [Link]());
}
}

123
333
012
000

Q5
Predict the output of the code given below.
class Printer {
static {
[Link]("Static block in Printer class");
}

public static void display(String message) {


[Link](message);
}
}

public class Tester {


public static int sampleVariable = 1;

static {
[Link]("Static block in Tester class");
}

public static void main(String[] args) {


sampleVariable++;
Printer p = null;
[Link]("In main");
[Link]("The value of sample variable is: " + sampleVariable);
}
}

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) {
[Link]([Link]);
}

public static void main(String args[]) {


Tester tester = new Tester(20);
[Link]([Link]);
}
}

20 20
00
compilation error: cannot use 'this' inside main
20 0

Common questions

Powered by AI

The sequence of outputs, 'Static block in Tester class; Static block in Printer class; In main; The value of sample variable is: 2,' is due to static blocks being executed when the class is first loaded. The 'Printer' class's static block only executes when referenced through a static method call .

Static variables, like 'id', allow shared state across all instances, meaning each new instance updates the shared static variable instead of having instance-specific values, which can be critical for counting or state-sharing applications .

The compilation error in a static block assigning a value to a non-static variable is because static blocks cannot access instance variables directly, leading to a 'non-static variable cannot be referenced from a static context' error .

The output of the code is '3 3 3'. This happens because 'id' is a static variable shared across all instances. Each 'Computer' object increments 'counter', setting 'id = ++counter', thus 'id' remains 3 for all instances .

Statements 3 and 4 are correct: Static blocks get executed only once, and static variables and methods can be accessed without creating an object of the class. Statement 1 is incorrect because static members can be accessed by non-static methods. Statement 2 is incorrect as non-static members cannot be accessed directly by static methods .

The output can be predicted by understanding that non-static members can't be accessed by static blocks, leading to specific points where execution either proceeds or fails, depending on the initialization success of the static variables and blocks .

Using 'this' inside the 'main' method causes a compilation error because 'this' refers to the current instance of a class and 'main' is a static method, which cannot use instance references directly .

In the given Java class 'Car', there is 1 static variable, 2 instance variables, and 1 local variable .

Static blocks are executed in the order they appear when the class is loaded. For instance, if a static block in a class is before another, it executes first, which affects the setup and initialization parts seen during early outputs .

It is important because modifying static variables in a constructor leads to unintended shared state across instances, undermining encapsulation and causing unpredictable behaviors as each constructor call may alter the application-wide shared state .

You might also like