Lesson 3
Lesson 3
class Person{
private String fname;
private String lname;
Person(String fname, String lname)
{
this.fname = fname;
this.laname = lname;
}
Main(){
// Status of the object – Current value should by the object
Person p1 = new Person(“Renuka”,”Mohanraj”); P1 –
Person p2 = new Person(“Renuka”,”Mohanraj”); 00ab
p1 = new Person(“Renu”,”Mohan”);
Person p3 = p2;
P2 –
} 0012
Name Matching: The constructor's name must exactly match the class name.
No Return Type: Constructors do not have a return type, not even void.
Implicit Constructor: If no constructor is defined, Java provides a default no-argument
constructor.
Multiple Constructors: A class can have multiple constructors, each with different parameter
lists (constructor overloading).
Access Modifiers: Constructors can use any access modifier: public, protected, private, or
default (package-private).
this(): The this() keyword calls another constructor in the same class. They must be the first
statement in a constructor.
this: The this keyword can be used to call the instance fields and methods.
Initialization: Constructors are used to initialize objects when they are created.
//Static Context: Constructors cannot be static, abstract, or final.
Object Creation: Constructors are called implicitly when an object is created using the new
keyword.
Date and Gregorian Calanders are Mutable.
Dateà Current date
Date today = new Date();
Date sday = new Date(10005);
Sday.getTime()
Record Class
• A private final field with the same name and declared type as the record
component. This field is sometimes referred to as a component field.
• Getters are length() and width().
Here, the explicit canonical constructor (using the compact form) allows you to add custom
validation while still automatically assigning the record components.
Final Keyword
Enum --> Better alternative for public static final constants and to get compile time verification.
Boxed Primitive
Memory Allocations
• Stack Memory:
o Local Variables:
§ x = 10 (in main() method)
§ a (in printdoubled() method)
o Method Calls:
§ main(String[] args)
§ printdoubled(int a)
o References:
§ ob (reference to DateAPIsMutability object)
§ s1 (reference to String object)
§ s2 (reference to String object)
• Heap Memory:
o Instance Fields:
§ data = 50 (for each Memory Allocation instance)
o Instance of DateAPIsMutability class (pointed to by ob)
o String Pool:
o Static Fields:
§ static int_Count = 0;
Sample Code and its Memory allocation
The JVM's garbage collector (GC) is highly optimized and manages memory efficiently without
manual intervention. Avoid System.gc() in most cases.
• Avoid keeping unnecessary static references, as they persist throughout the application's
lifecycle.
• Java's Approach: Java does not support call by reference. Even with objects, the
reference is passed by value, not the actual object.
Homework –Hints
class Customer{
Fname;
Lanmae;
Ssn;
Address badd; // has a replations
Address sadd;
}
class Address{ }
class Main ()
{
Customer c1 = new Customer(“fn”,”nl”,”ssn”);
Customer c2 = new Customer(“fn”,”nl”,”ssn”);
Customer c3 = new Customer(“fn”,”nl”,”ssn”);
C1.setBadd(a1);
}
Problem 2: You won’t get the same sample output, due to current date is different.
Example Task 3 you can choose your way of nice formatting must understand by the
user.
Another Suggestion: You can go with only one class. Or you can create an Event class
and Test class. All is up to you.
Inside a class
1. Static block
Static{
}
2. Static initialization
Static int x = 10;
Will initialized only once for class level
3. Instance fields
int k = 10;
4. Instance methods
5. Instance block / Anonymous block
{
}
Will execute before the constructor execution
6. Constructor block
Instance fields and instance block have same priority, in which order you specified