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.
π¨βπ« Trainer: Nantha from Payilagam
π€ 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)