0% found this document useful (0 votes)
26 views10 pages

Java DSA Notes

A class is a blueprint that defines attributes and behaviors, while an Entity class maps to a database table, and a Model class represents data/business logic in MVC architecture. When an object is created, memory is allocated, and it becomes a real instance of the class. The main() method in Java is public, static, and void to ensure accessibility, no object creation for execution, and no return value to the JVM.

Uploaded by

gurudilp456
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)
26 views10 pages

Java DSA Notes

A class is a blueprint that defines attributes and behaviors, while an Entity class maps to a database table, and a Model class represents data/business logic in MVC architecture. When an object is created, memory is allocated, and it becomes a real instance of the class. The main() method in Java is public, static, and void to ensure accessibility, no object creation for execution, and no return value to the JVM.

Uploaded by

gurudilp456
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

1. What is a Class?

A class is like a blueprint or template.


It defines how something should look (its attributes) and what it can do (its behaviors).

For example:

class Car {

// Attributes (properties)

String brand;

String color;

int speed;

// Behaviors (methods)

void drive() {

[Link](brand + " is driving at " + speed + " km/h");

void stop() {

[Link](brand + " has stopped.");

2. Entity Class (in database context)

• In Hibernate / JPA / Spring Boot, an Entity class is a special class mapped to a database table.

• Each object of that class represents a row in the table.


Example:

import [Link].*;

@Entity

@Table(name = "students")

public class Student {

@Id

@GeneratedValue(strategy = [Link])

private int id;

private String name;

private int age;

// getters & setters

This means:

• Table: students

• Row: each Student object

• Columns: id, name, age

3. Model Class (in MVC architecture)

• In MVC (Model-View-Controller), a Model represents the data and business logic.

• It may or may not be tied directly to a database.

• Example: A User class used to transfer data between frontend and backend.

4. Real-Life Object

• When we create an object (using new keyword), we are creating a real instance of the class.
Example:

Car myCar = new Car();

[Link] = "Tesla";

[Link] = "Red";

[Link] = 120;

[Link]();

myCar is a real-life object created from the Car blueprint.

Summary:

• Class → Blueprint (defines attributes + behaviors).

• Entity → A class mapped to a database table.

• Model → A class that represents data/business logic in an application (used in MVC).

• Object → A real instance of a class in memory.

Real-Life Analogy: Student in a College

1. Class (Blueprint)

Think of the admission form in a college.

• It has fields: id, name, age, course.

• It defines what information every student must have.

In code:

class Student {

int id;

String name;

int age;

String course;

This is just a blueprint, no real student yet.


2. Entity (Database Table)

In the college office, all students’ data is stored in a database table called students.

id name age course

1 Rahul 20 CSE

2 Priya 21 ECE

3 John 22 MECH

Each row in the table is a student record.


The Student Entity class in code is mapped to this table.

@Entity

@Table(name="students")

class Student {

@Id

@GeneratedValue(strategy=[Link])

int id;

String name;

int age;

String course;

3. Model (MVC layer)

Suppose the college has a website where students can log in and see their details.

• The Model is the Student object we send to the frontend (View).

• Example: When Rahul logs in, backend fetches his details and sends a Student model.

The Model carries data between backend and frontend.

4. Object (Real Instance)

When a real student joins the college (say Rahul), the admission form gets filled with actual values.

In code:

Student s1 = new Student();

[Link] = 1;

[Link] = "Rahul";
[Link] = 20;

[Link] = "CSE";

Now s1 is a real object in memory.

Putting It All Together

• Class → Blueprint (Admission form)

• Entity → Database table (students)

• Model → Data used in application/website (Rahul’s info shown on portal)

• Object → Real student (Rahul sitting in class)

What happens when we create a class.


1. When we define a class
Example in Java:
class Student {
int id;
String name;
void study() {
[Link](name + " is studying");
}
}
What happens:
• The compiler reads the class definition.
• It does not create memory for id, name, or study() yet.
• It only stores the blueprint (a design for future objects)
2. When we create an object (instance of a class)
• Example:
• Student s1 = new Student()

What happens internally:


1. Memory Allocation
o Space is reserved in Heap memory for variables (id,
name).
o Default values are assigned (0 for int, null for
objects like String).
2. Constructor Call
o If a constructor exists, it is executed.
o If not, Java provides a default constructor.
3. Reference Creation
o s1 (on Stack memory) stores the reference (address) of
the object in the Heap.

3. Multiple Objects
If we create more objects:
Student s2 = new Student();
A new separate memory block is created in Heap for s2.
Each object has its own copy of variables (id, name), but all objects share the
same methods (study()).
Summary
• Defining a class = only blueprint (no memory yet).
• Creating an object = memory allocated in Heap + constructor run +
reference created in Stack.
• Each object = separate copy of variables, shared methods.
Why main() is static, void and public.
In Java, the entry point of any program is the main() method:
public static void main(String[] args)
Now let’s analyze why it is public static void:

1. public
• Reason: It must be accessible to the Java Virtual
Machine (JVM) from outside the class.
• If it were not public, the JVM would not be able to call it
because of access restrictions.
• Example: If main() were private, the program would
compile but give an error at runtime (since JVM can’t
access it).

2. static
• Reason: JVM needs to call main() without creating an
object of the class.
• When a program starts, no object exists yet.
• Making it static means JVM can directly call it using the
class name.
• Example:
• [Link](args); // JVM internally calls like this
3. void
• Reason: The main() method does not return anything to
the JVM.
• Once the program ends, JVM doesn’t need a return value—
it just needs the program to finish execution.
• Instead, termination codes (like success 0 or error codes)
are handled by [Link]() if needed, not via main()
return.

So putting it all together:


• public → JVM can access it.
• static → No object needed to start execution.
• void → Nothing is returned to JVM.

In the statement [Link]()


What is System and to which package it belongs to?
What is out?
From which class the print() belongs to ?
-------------------------------------------------------------------------------
1. What is System and to which package it belongs to?
• System is a final class in [Link] package.
• It cannot be inherited.
• It provides standard input, output, error streams,
environment variables etc.
• Package:
• package [Link];
public final class System { ... }

2. What is out?
• out is a public static final field inside the System class.
• It is of type PrintStream.
• It represents the standard output stream (usually the
console).
• Declaration inside System:
• public static final PrintStream out;
3. From which class does print() belong to?
• The print() method belongs to the PrintStream class (in
the [Link] package).
• Signature (one of many overloaded methods):
• public void print(String s)
• The PrintStream class provides both print() and println()
methods for displaying output.
So in short:
• System → class in [Link] package
• out → static object of type PrintStream inside System
• print() → method of PrintStream (in [Link] package)

You might also like