Java Classes and Objects, Declaring Objects, Constructors: Dr. Kumud Tripathi
Java Classes and Objects, Declaring Objects, Constructors: Dr. Kumud Tripathi
● A class is a blueprint that defines the variables and methods common to all objects of a certain
kind.
● Example: ‘your dog’ is a object of the class Dog.
● An object holds values for the variables defines in the class.
● An object is called an instance of the Class
Object Creation
String s;
● In Java, when an object is no longer accessible through any variable, it is eventually removed
from the memory by the garbage collector.
● Garbage collector is parts of the Java Run-Time Environment.
Constructors
1. Default Constructor
2. No-arg Constructor
3. Parameterized Constructor
Default Constructor
<class_name>()
—--------------------
A constructor is used to initialize the state of an A method is used to expose the behavior of
object. an object.
A constructor must not have a return type. A method must have a return type.
The Java compiler provides a default constructor if The method is not provided by the compiler
The constructor name must be same as the class The method name may or may not be same