■ What is a Wrapper Class?
A wrapper class is a class that converts a primitive data type into an object.
wrapper class :
| Primitive | Wrapper class |
| --------- | ------------- |
| `int` | `Integer` |
| `float` | `Float` |
| `double` | `Double` |
| `char` | `Character` |
| `boolean` | `Boolean` |
| `byte` | `Byte` |
| `short` | `Short` |
| `long` | `Long` |
main reason for wrapper class:
ArrayList<Integer> list = new ArrayList<>();
[Link](10); // int → Integer (auto-boxing)
■ Autoboxing & Unboxing
Java automatically converts between primitive and wrapper.
Autoboxing (primitive → object):
Integer x = 10;
Unboxing (object → primitive):
int y = x;
# can be assigned to null values:
Integer x = null; // valid
int y = null; // ■ error
example :
class WrapperExample {
public static void main(String[] args) {
Integer a = 10; // autoboxing
int b = a; // unboxing
[Link](a);
[Link](b);
}
}
■static variables and static methods :
static variables are not
07/01/2026
■confusion2 :
1.
class Student{
int rollno=10;
Student(){
rollno=rollno
[Link](rollno+"@");
}
}
-----> output for this is there is no parameter inside the constructor so the value which is 20 in main
scope does not pass to the constructor and 10 in class Student will be sent to the first print
statement
and it prints 10@ after that the flow goes to the main function and the no change is occurred in the
class
Student due to this keyword doesnot present in student thus [Link] also prints 10
10@10
class confusion2{
public static void main(String[] args){
int rollno=20;
Student s=new Student();
[Link]([Link]);
}
}
[Link] Student{
int rollno=10;
String name;
Student(){
[Link]=rollno;
[Link](rollno+"@");
}
}
same reason as the first program
class confusion2{
public static void main(String[] args){
String name="parthi";
int rollno=20;
Student s=new Student();
[Link]([Link]);
}
}
3.
class Student{
int rollno=10;
String name;
Student(int rollno){
rollno=rollno;
[Link](rollno+"@");
}
}
here rollno is passed as a parameter inside the constructor and the 10 declared in Student is
changed
10 into 20 in the line rollno=rollno but the change does not get reflected in student class and 10
which is declared inside in the student does not get change and the [Link] present in the main
function only prints the 10 [Link] of rollno=rollno(20) exists only within constructor not in
main function.
20@10
class confusion2{
public static void main(String[] args){
String name="parthi";
int rollno=20;
Student s=new Student(rollno);
[Link]([Link]);
}
}
4.
class Student{
int rollno=10;
String name;
Student(){
[Link]=rollno;
[Link](rollno+"@");
}
}
same rule as the but the this arrives in the [Link]=rollno this makes the first sop to print the
value 20 by changing the value from 10 to 20 as in 3 program and also this makes an permanent
change
in the int rollno in student class so the second sop in main function which prints sop([Link])
which always prints the class variable print the changed value [Link] exits within [Link]
due to "this" keyword the value change also reflects in the class Student.
class confusion2{
public static void main(String[] args){
String name="parthi";
int rollno=20;
Student s=new Student();
[Link]([Link]);
}
}
In static method we cannot actually use the this keyword as the static is itself not an object related
one it is something related to class but the "this"
keyword related to object.
11/01/2026
[Link] can able to have the static method inside the non static method there will be no error for that.
[Link] class in java program cannot be a static class but inner class can be static.
[Link] blocks runs only one side in the program
■ Program 1 (confusion4)
class innerclass{
static String name; ---> there will be change in name as it is declared as a static and static
something which is related to class so change no copy
is made for String name for each object creation and change always reflected and only final
changes is printed as output
public innerclass(String name){
[Link] = name;
}
}
public class confusion4{
public static void main(String[] args){
innerclass a = new innerclass("parthi");
innerclass b = new innerclass("rahul");
[Link]([Link]);
[Link]([Link]);
}
}
■ What is happening?
name is static
Static variables are shared by all objects
There is only ONE copy of name in memory
■ Execution flow
a is created → name = "parthi"
b is created → name = "rahul" (overwrites previous value)
Both [Link] and [Link] refer to the same static variable
■ Output
rahul
rahul
■ Program 2 (confusion3)
class confusion3{
static class innerclass{ ------> the static for the inner class is because without static we have to
create an object for the outer class so
static is used for innerclass to work independently without the class confusion3 else an object for
outer
class should be created.
String name;
public innerclass(String name){
[Link] = name;
}
}
public static void main(String[] args){
innerclass a = new innerclass("parthi");
innerclass b = new innerclass("rahul");
[Link]([Link]);
[Link]([Link]);
}
}
■ What is happening?
name is NOT static
Each object has its own separate copy of name
[Link] means current object’s variable
■ Execution flow
a stores → "parthi"
b stores → "rahul"
Values are independent
■ Output
parthi
rahul
from here static stuff understanding:
■ ONE-LINE EXAM / INTERVIEW ANSWER
Static members are class-level and use compile-time binding because they are resolved using
reference type, not object type.
■ Statement to explain
“Static stuff are resolved during compile time; they are class-level methods.”
We’ll break this into 3 parts:
What static really means
What is resolved at compile time
What class-level means
1■■ What does static mean?
static int x;
static void show() {}
✔ Belongs to the class, not to objects
✔ Only one copy exists
✔ Can be accessed using class name
ClassName.x;
[Link]();
This is why we say:
Static members are class-level
2■■ What does “resolved at compile time” REALLY mean?
■■ This does NOT mean:
value is fixed at compile time ■
static code runs at compile time ■
✔ Correct meaning:
The method call or variable access is decided at compile time, not runtime.
3■■ Static methods and compile-time binding
Example
class A {
static void show() {
[Link]("A");
}
}
class B extends A {
static void show() {
[Link]("B");
}
}
public class Main {
public static void main(String[] args) {
A obj = new B();
[Link]();
}
}
■■ Output
A
■ Why?
Static methods are NOT overridden
They are hidden
Compiler looks at reference type (A), not object type (B)**
✔ Decision made at compile time
4■■ Non-static (runtime resolution)
class A {
void show() {
[Link]("A");
}
}
class B extends A {
void show() {
[Link]("B");
}
}
public class Main {
public static void main(String[] args) {
A obj = new B();
[Link]();
}
}
■■ Output
B
✔ Runtime decision
✔ Dynamic binding
✔ Depends on actual object
5■■ Static variables: compile-time or runtime?
static int x = 10;
Memory allocated when class is loaded (runtime)
Access resolved at compile time
[Link](ClassName.x);
✔ Compiler knows exactly which x
6■■ Why static methods cannot use this
static void show() {
[Link](this.x); // ■ ERROR
}
■ No object
■ No instance context
Because static belongs to class, not object.
dot operator: dot operator is generally binds the instance variable in instance method with the
reference variable.
12/01/2026:
class Parent {
int a = 10;
void show() {
[Link]("Parent show");
}
}
class Child extends Parent {
int b = 20;
void display() {
[Link]("Child display");
}
}
public class extend {
public static void main(String[] args) {
// ■ Case 1: Parent reference, Child object
Parent p = new Child();
[Link](p.a); // ■
[Link](); // ■
// p.b; // ■
// [Link](); // ■
[Link]("-----");
// ■ Case 2: Child reference, Parent object
//Child c = new Parent(); // ■ COMPILE ERROR
}
}
15/01/2026:
Using a parent’s variable in a child class REQUIRES extends, unless it is passed as a parameter.
wrong example :
class Parent {
int x;
}
class Child {
void show() {
[Link](x);
}
}
correct example:
class Parent {
int x = 10;
}
class Child extends Parent {
void show() {
[Link](x);
}
}
class Test {
public static void main(String[] args) {
Child c = new Child();
[Link]();
}
}
super() method should be in a class which has constractor of parameters only for constructor of no
parameter doesnot need super() jvm automatically calls the super()
16/01/2026
overriding :
■ Dynamic Method Dispatch (Java)
Dynamic Method Dispatch is the mechanism by which Java decides which overridden method to
call at runtime, based on the actual object, not the reference type.
■ It is the core of runtime polymorphism.
Dynamic Method Dispatch is the process of resolving an overridden method call at runtime based
on the object type.
■ Key Conditions
Dynamic method dispatch works only when:
Inheritance exists
Method overriding is present
Upcasting is used (parent reference → child object)
■ How it works internally
Compiler checks reference type (is method allowed?)
JVM checks object type at runtime
JVM executes the overridden method