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

Java Static Keyword Lab Solutions

The document explains the use of the static keyword in Java through various examples, including static variables, methods, and blocks. It highlights how static members are shared across all instances of a class and cannot directly access non-static members. Additionally, it discusses the execution order of static blocks and provides viva points for each example.
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)
3 views4 pages

Java Static Keyword Lab Solutions

The document explains the use of the static keyword in Java through various examples, including static variables, methods, and blocks. It highlights how static members are shared across all instances of a class and cannot directly access non-static members. Additionally, it discusses the execution order of static blocks and provides viva points for each example.
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

Java Static Keyword – Questions, Code, Output,

Explanation, Viva

1) Static Variable – Counting Objects


Question: Create a Student class with static count to track number of objects.

class Student {
String name;
static int count = 0;

Student(String name) {
[Link] = name;
count++;
}
}

class Main {
public static void main(String[] args) {
new Student("Anita");
new Student("Rahul");
new Student("Priya");

[Link]("Total Students: " + [Link]);


}
}

Output

Total Students: 3

Explanation: Static variable is shared across all objects and increments with each object creation.

Viva Points: - Static variable → one copy per class - Access using [Link]

2) Static Method – Utility Calculator


Question: Create static methods add() and square() and call without object.

class Calculator {
static int add(int a, int b) {

1
return a + b;
}

static int square(int x) {


return x * x;
}
}

class Main {
public static void main(String[] args) {
int sum = [Link](10, 20);
int sq = [Link](5);

[Link]("Sum: " + sum);


[Link]("Square: " + sq);
}
}

Output

Sum: 30
Square: 25

Explanation: Static methods belong to class, not object.

Viva Points: - Static method cannot access non-static directly

3) Static Block – Initialization


Question: Initialize companyName using static block.

class Company {
static String companyName;

static {
companyName = "InnovAI Technologies";
[Link]("Static Block Executed");
}
}

class Main {
public static void main(String[] args) {
[Link]("Company Name: " + [Link]);
}
}

2
Output

Static Block Executed


Company Name: InnovAI Technologies

Explanation: Static block executes once when class loads.

Viva Points: - Runs before main()

4) Static vs Non-Static Access


Question: Show error and fix when accessing non-static variable inside static method.

class Test {
int x = 10;
static int y = 20;

static void show() {


// [Link](x); // ERROR

Test obj = new Test();


[Link]("x: " + obj.x);
[Link]("y: " + y);
}

public static void main(String[] args) {


show();
}
}

Output

x: 10
y: 20

Explanation: Static method cannot directly access instance variable.

Viva Points: - Need object for non-static access

5) Static Block Execution Order


Question: Predict output of multiple static blocks.

3
class Demo {
static {
[Link]("Static Block 1");
}

static {
[Link]("Static Block 2");
}

public static void main(String[] args) {


[Link]("Main Method");
}
}

Output

Static Block 1
Static Block 2
Main Method

Explanation: Static blocks execute in order before main().

Viva Points: - Execution order matters

One-Line Exam Rule


Static members belong to the class, not objects, and are shared across all instances.

End of Document

You might also like