0% found this document useful (0 votes)
6 views17 pages

PSOOP-Lecture 02-24-25

This document provides an overview of classes and objects in Java, detailing their definitions, components, and the concepts of constructors and garbage collection. It explains the use of access modifiers, the creation and lifetime of objects, and the scope of variables. Additionally, it covers command line arguments and their usage in Java applications.

Uploaded by

nikahatkazi
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)
6 views17 pages

PSOOP-Lecture 02-24-25

This document provides an overview of classes and objects in Java, detailing their definitions, components, and the concepts of constructors and garbage collection. It explains the use of access modifiers, the creation and lifetime of objects, and the scope of variables. Additionally, it covers command line arguments and their usage in Java applications.

Uploaded by

nikahatkazi
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
You are on page 1/ 17

PSOOP(JAVA) – LECTURE 02

CLASSES AND OBJECTS IN JAVA


-Compiled by Nikahat Mulla
AGENDA

 Classes
 Objects

 Constructors

 Garbage Collection in Java

 Scope of Variables

 Command Line Arguments

2
CONCEPT OF CLASS
 A class is a description/blueprint/template of a group of objects
with common properties (attributes) & behavior (operations)
 An object is a real world entity which is an instance of a class
e.g. Mary is an object of Student class
Jane is an object of Student class

Mary 1001 Jane 1002

name rollNo name rollNo


CONSTITUENTS OF A CLASS Data Members
public class Student {
(State)
private int rollNo;
private String name;
No argument
Student(){ Constructor
//initialize data members
}
Student(String nameParam){ Parameterized
name = nameParam; Constructor
}
public int getrollNo (){
return rollNo;
} Method
} (Behavior)

The main method may or may not be present depending on whether the class is a starter class

note
ACCESS MODIFIERS – PRIVATE & PUBLIC
 Four Access Modifiers: Default is
 private NOT a
 protected keyword in
 public Java
 default

 Data members are always kept private


 Accessible only within the class

 The methods which expose the behavior of the object are kept public
 However, we can have helper methods which are private

 Key features of Object Oriented Programs


 Encapsulation (code & data bound together)
 State (data) is hidden & Behavior (methods) is exposed to external
world
CREATING OBJECTS
 The new operator creates an object & returns a reference to
it
 Memory allocation of objects happens in the heap area
 Reference returned can be stored in reference variables
Student obj1; obj1 is a reference variable
obj1 = new Student();
Or
Student obj2 = new Student(“John”);

new keyword creates


obj1: Student obj2: Student an object and returns
a reference to it
name name John

note
CONSTRUCTORS
 Special methods used to initialize a newly created object

 Called just after memory is allocated for an object

 Initialize objects to required or default values at the time of object


creation

 Not mandatory to write a constructor for each class

 A constructor
 Has the same name as that of the class

 Doesn’t return any value, not even void

 May or may not have parameters (arguments)

 If a class does not have any constructor, the default constructor is


automatically added
CONSTRUCTORS (CONTD…)
 In the absence of a user defined constructor, the
compiler initializes member variables to its
default values

 Numeric data types are set to 0

 Char data types are set to null character (‘\0’)

 Reference variables are set to null


LIFETIME OF OBJECTS
obj1

Student obj1 = new Student(); 1


Student obj2 = new Student();
Both Student objects now live on the heap Heap
2
→ References : 2
→ Objects :2 obj2

obj1

Student obj3 = obj2;


→ References : 3 1

→ Objects :2 obj3
2
Heap

obj2
LIFETIME OF OBJECTS (CONTD…)
obj1
obj3 = obj1;
→ References : 3
1
obj3
→ Objects :2
2
Heap

obj2

obj1
obj2 = null;
→ Active References : 2 1

→ Null References :1 obj3 2


→ Reachable Objects : 1 Heap

→ Abandoned objects : 1
obj2

Null Reference This object can be


garbage collected
GARBAGE COLLECTION
 In C/C++, it is the programmer’s responsibility to de-
allocate the dynamically allocated memory using the free()
function

 JVM automatically de-allocates memory (Garbage


Collection)

 An object which is not referred by any reference variable is


removed from memory by the Garbage Collector

 Primitive types are not objects & cannot be assigned null

note
SCOPE OF VARIABLES
 Instance Variables (also called Member Variables)
 Declared inside a class
 Outside any method or constructor
 Belong to the object
 Stored in heap area with the object to which they belong
to
 Lifetime depends on the lifetime of object

 Local Variables (also called Stack Variables)


 Declared inside a method
 Method parameters are also local variables
 Stored in the program stack along with method calls
and live until the call ends
note
SCOPE OF VARIABLES (CONTD…)
 If we don’t initialize instance variables explicitly, they are
awarded predictable default initial values, based only on the type
of the variable
Type Default Value
boolean false
byte (byte) 0
short (short) 0
int 0
long 0L
char \u0000
float 0.0f
double 0.0d
object reference null

 Local variables are not initialized implicitly


SCOPE OF VARIABLES (CONTD…)
class Student{
rollNo and name are
int rollNo; instance variables to be
stored in the heap
String name;
public void display (int z){
int x=z+10;
}
}
z and x are local
variables to be stored in
the stack
SCOPE OF VARIABLES (CONTD…)
COMMAND LINE ARGUMENTS
 Information that follows program’s name on the command line
when it is executed
 This data is passed to the application in the form of String
arguments

class Echo {
public static void main (String args[]) {
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
Try this: Invoke the Echo application as
follows
C:\> java Echo Drink Hot Java
Drink
Hot
Java

note
THANK YOU

You might also like