As part of my Java learning journey with my trainer, we were given a set of conceptual and tricky questions to answer.
The goal was not just to memorize answers but to understand Java fundamentals deeply, especially around:
Constructors
Inheritance
Java Features
var keyword
Object Creation
In this blog, I’m sharing the first set of questions along with explanations and code analysis.
1️⃣ How many ways can we initialize an instance variable in Java?
Instance variables can be initialized in multiple ways:
- Direct Initialization inside the class
class Example {
int x = 10;
}
Here the value is assigned directly during declaration.
- Using a Constructor
class Example {
int x;
Example() {
x = 20;
}
}
When the object is created, the constructor assigns the value.
- Using a Method
class Example {
int x;
void setValue() {
x = 30;
}
}
The value is initialized by calling a method.
- Using an Instance Initialization Block
class Example {
int x;
{
x = 40;
}
}
This block runs before the constructor executes.
Key Concept
Instance variables belong to object memory (heap memory).
Every object gets its own copy of instance variables.
2️⃣ What is a Constructor in Java?
A constructor is a special method used to initialize objects when they are created.
Important Properties
Constructor name must be the same as the class name
It does not have a return type
It is automatically executed when an object is created
Used to initialize instance variables
Example
class Student {
int id;
Student(int id) {
this.id = id;
}
public static void main(String[] args) {
Student s1 = new Student(101);
System.out.println(s1.id);
}
}
Code Explanation (Step by Step)
Step 1 – Class Loading
JVM loads the class Student.
Step 2 – Object Creation
Student s1 = new Student(101);
This statement performs three operations.
1️⃣ Memory is allocated in Heap Memory
2️⃣ Constructor is called
3️⃣ Reference variable stores object address
Step 3 – Constructor Execution
Student(int id)
The value 101 is passed to the constructor.
Step 4 – thiskeyword
this.id = id;
Explanation:
Variable Meaning
this.id instance variable
id constructor parameter
So Java assigns:
instance variable = parameter value
Step 5 – Printing Value
System.out.println(s1.id);
Output:
101
3️⃣ How to Achieve a Constructor? (Object Creation)
Constructors are executed when objects are created using the new keyword.
Example:
class Step1 {
int x;
Step1(int x) {
this.x = x;
}
public static void main(String[] args) {
Step1 ref1 = new Step1(10);
System.out.println(ref1.x);
}
}
Deep Explanation of the Code
Line 1
Step1 ref1
This is a reference variable.
It does not store the object itself — it stores the memory address of the object.
Line 2
new Step1(10)
This does two things:
- Creates object
- Calls constructor
JVM Execution Flow
Step1 ref1 = new Step1(10);
JVM internally performs:
- Allocate memory in Heap
- Call constructor
- Assign reference to ref1
Constructor Execution
Step1(int x)
Value 10 is passed to constructor.
Then:
this.x = x
Now instance variable becomes:
x = 10
Final Output
10
4️⃣ What is Constructor Overloading?
Constructor overloading means having multiple constructors in the same class with different parameters.
Example:
class Student {
Student() {
System.out.println("Default Constructor");
}
Student(int id) {
System.out.println("Student ID: " + id);
}
public static void main(String[] args) {
new Student();
new Student(101);
}
}
Output
Default Constructor
Student ID: 101
Why Constructor Overloading?
It allows objects to be created in different ways.
Example:
Student()
Student(int id)
Student(int id, String name)
5️⃣ Is Default Constructor Visible?
Yes.
If we do not define any constructor, the Java compiler automatically provides a default constructor.
Example:
class Test {
}
Compiler internally creates:
Test() {
}
6️⃣ How to Call a Constructor?
Constructors are called during object creation.
Example:
new ClassName();
or
ClassName ref = new ClassName();
Example program:
class Demo {
Demo() {
System.out.println("Constructor Called");
}
public static void main(String[] args) {
new Demo();
}
}
7️⃣ Real World Examples of Constructors
Bank Account
When creating a bank account:
Account number
Customer name
Balance
All values can be initialized using a constructor.
Hospital System
When registering a patient:
Patient name
Patient ID
Illness
These details can be initialized during object creation.
8️⃣ What is Inheritance?
Inheritance is a mechanism where one class acquires the properties and behaviours of another class.
This helps achieve code reusability.
Example
- Parent Class → Employee
- Child Class → Developer
Developer automatically inherits employee features.
9️⃣ Why Use Inheritance?
Inheritance helps with:
- Code Reusability
- Reduced Redundancy
- Easy Maintenance
- Better Code Structure
🔟 How to Achieve Inheritance?
Inheritance is implemented using the extends keyword.
Example:
class Parent {
}
class Child extends Parent {
}
Here Child inherits properties from Parent.
1️⃣1️⃣ Real World Examples of Inheritance
Example 1 – Company Structure
Employee
↓
Developer
↓
Tester
All share common employee properties.
Example 2 – Vehicle System
Vehicle
↓
Car
↓
Truck
Common properties:
Engine
Speed
Fuel
1️⃣2️⃣ How many types of inheritance in Java?
Java supports 5 types of inheritance conceptually.
1️⃣3️⃣ Types of inheritance
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance (via interfaces only)
- Hybrid Inheritance (combination)
1️⃣4️⃣ Sample program for single inheritance
class Parent {
String name;
}
class Child extends Parent {
public static void main(String[] args) {
Child obj = new Child();
obj.name = "Rajiv";
System.out.println(obj.name);
}
}
1️⃣5️⃣ Which inheritance is not supported in Java?
Multiple inheritance using classes is not supported in Java.
Reason:
It causes the Diamond Problem (Ambiguity problem).
Example conflict:
Class A → method()
Class B → method()
Class C extends A, B
Java cannot decide which method to inherit.
1️⃣6️⃣ Sample program for multilevel inheritance
class Parent {
String name = "Rajiv";
}
class Child extends Parent {
}
class Child2 extends Child {
public static void main(String[] args) {
Child2 obj = new Child2();
System.out.println(obj.name);
}
}
1️⃣7️⃣ Latest Version of Java
The latest version of Java currently is Java 25 (released by Oracle as part of the Java SE platform).
Java releases two versions every year:
- LTS (Long Term Support) versions
- Non-LTS versions
Example LTS versions:
- Java 8
- Java 11
- Java 17
- Java 21
1️⃣8️⃣ Java Features
Some important features of Java include:
- Simple – Easy to learn and use
- Object-Oriented – Based on OOP concepts like class and objects
- Platform Independent – "Write Once, Run Anywhere"
- Secure – No direct memory access like pointers
- Robust – Strong memory management and exception handling
- Multithreaded – Supports multiple threads
- Portable – Can run on different systems
- Dynamic – Classes can be loaded dynamically
1️⃣9️⃣ Is Java 100% Object Oriented?
Java is not 100% object-oriented.
Reason:
Java supports primitive data types.
Example:
int
char
float
boolean
To overcome this, Java provides Wrapper Classes.
Example:
int → Integer
char → Character
2️⃣0️⃣ What is var Keyword?
Introduced in Java 10.
It allows type inference for local variables.
Example:
var x = 10;
Compiler automatically detects the type as int.
Restrictions of var:
var cannot be used for
- Instance variables
- Method parameters
- Return types
- Only allowed for local variables.
2️⃣1️⃣ Can We Run a Java Class Without main() Method?
Normally Java programs require the main() method because JVM starts execution from main().
However, there are special cases where a program can run without main():
Examples:
Static blocks (in older Java versions)
Applets
Servlets
JavaFX
But standard standalone Java programs must have a main() method.
2️⃣2️⃣ Do You Use var Keyword in Java?
Yes.
The var keyword is used to declare local variables with type inference.
Example:
var x = 10;
Here the compiler automatically understands the type as int.
var can only be used for local variables inside methods.
2️⃣3️⃣ If We Create a Variable Using var, Where Will JVM Allocate Memory?
Since var can only be used for local variables, the memory is allocated in stack memory.
Example:
public static void main(String[] args) {
var x = 10;
}
x is a local variable, so it is stored in the stack.
2️⃣3️⃣ Can We Use var as a Global Variable?
The var keyword cannot be used for:
- Instance variables
- Static variables
- Method parameters
- Return types
- It is allowed only inside methods (local variables).
Example ❌ Invalid:
class Test {
var x = 10; // Error
}
2️⃣5️⃣ What Is the Size of var Data Type?
var does not have a fixed size.
Because var is not a data type.
It is only a type inference keyword.
Example:
var x = 10; // int (4 bytes)
var y = 10.5; // double (8 bytes)
var name = "Java"; // String object
The actual size depends on the inferred type.
2️⃣6️⃣ Which Company Releases Java Versions?
Originally Java was developed and released by Sun Microsystems.
Later Oracle Corporation acquired Sun Microsystems in 2010, and since then Oracle maintains and releases Java versions.
📌 Key Learning from Day 11
Today’s discussion helped me understand:
- Constructor fundamentals
- Constructor overloading
- Object creation flow
- Inheritance basics
- Java features
- var keyword behavior
Understanding how JVM executes constructors and object creation is crucial for writing clean Java programs.
🤖 A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainer’s explanations.

Top comments (0)