0% found this document useful (0 votes)
5 views8 pages

Unit II

The document explains the concepts of objects and classes in Java, highlighting that an object is a physical and logical entity with state, behavior, and identity, while a class is a logical blueprint for creating objects. It details how to declare classes, initialize objects through reference variables, methods, and constructors, and discusses constructor overloading and the usage of the 'this' keyword. Additionally, it covers garbage collection in Java, which automatically manages memory by identifying and deleting unused objects.

Uploaded by

bs290094
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Unit II

The document explains the concepts of objects and classes in Java, highlighting that an object is a physical and logical entity with state, behavior, and identity, while a class is a logical blueprint for creating objects. It details how to declare classes, initialize objects through reference variables, methods, and constructors, and discusses constructor overloading and the usage of the 'this' keyword. Additionally, it covers garbage collection in Java, which automatically manages memory by identifying and deleting unused objects.

Uploaded by

bs290094
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Objects and Classes in Java

An object in Java is the physical as well as a logical entity, whereas, a class in Java is
a logical entity only.

An object has three characteristics:

o State: represents the data (value) of an object.


o Behavior: represents the behavior (functionality) of an object such as
deposit, withdraw, etc.
o Identity: An object identity is typically implemented via a unique ID.
The value of the ID is not visible to the external user. However, it is
used internally by the JVM to identify each object uniquely.

An object is an instance of a class. A class is a template or blueprint


from which objects are created. So, an object is the instance(result) of a
class.

Object Definitions:

o An object is a real-world entity.


o An object is a runtime entity.
o The object is an entity which has state and behavior.
o The object is an instance of a class.

A class is a group of objects which have common properties. It is a template


or blueprint from which objects are created. It is a logical entity. It can't be
physical.

A class in Java can contain:

o Fields
o Methods
o Constructors
o Blocks
o Nested class and interface
Syntax to declare a class:
class <class_name>{
field;
method;
}
//Java Program to illustrate how to define a class and fields
//Defining a Student class.
class Student{
//defining fields
int id;//field or data member or instance variable
String name;
//creating main method inside the Student class
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference variable

System.out.println(s1.name);
}
}

There are 3 ways to initialize object in Java.

1. By reference variable
2. By method
3. By constructor

1) Object and Class Example: Initialization through


reference
Initializing an object means storing data into the object. Let's see a simple
example where we are going to initialize the object through a reference
variable.

class Student{
int id;
String name;
}
class TestStudent2{
public static void main(String args[]){
Student s1=new Student();
s1.id=101;
s1.name="Sonoo";
System.out.println(s1.id+" "+s1.name);//printing members with a white space
}
}
2) Object and Class Example: Initialization through
method
In this example, we are creating the two objects of Student class and initializing the
value to these objects by invoking the insertRecord method. Here, we are displaying
the state (data) of the objects by invoking the displayInformation() method.
1. class Student{
2. int rollno;
3. String name;
4. void insertRecord(int r, String n){
5. rollno=r;
6. name=n;
7. }
8. void displayInformation(){System.out.println(rollno+" "+name);}
9. }
10. class TestStudent4{
11. public static void main(String args[]){
12. Student s1=new Student();
13. Student s2=new Student();
14. s1.insertRecord(111,"Karan");
15. s2.insertRecord(222,"Aryan");
16. s1.displayInformation();
17. s2.displayInformation();
18. }
19. }

3) Object and Class Example: Initialization through a


constructor
In Java, a constructor is a block of codes similar to the method. It is called when an instance of
the class is created. At the time of calling constructor, memory for the object is allocated in the
memory.

It is a special type of method which is used to initialize the object.

Every time an object is created using the new() keyword, at least one constructor is called.

It calls a default constructor if there is no constructor available in the class. In such case, Java
compiler provides a default constructor by default.

Rules for creating Java constructor


There are rules defined for the constructor.

1. Constructor name must be the same as its class name


2. A Constructor must have no explicit return type
3. A Java constructor cannot be abstract, static, final, and synchronized

Types of Java constructors


There are two types of constructors in Java:

1. Default constructor (no-arg constructor)


2. Parameterized constructor

Example of default constructor


In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at th
time of object creation.

1. //Java Program to create and call a default constructor


2. class Bike1{
3. //creating a default constructor
4. Bike1(){System.out.println("Bike is created");}
5. //main method
6. public static void main(String args[]){
7. //calling a default constructor
8. Bike1 b=new Bike1();
9. }
10.}

What is the purpose of a default constructor?

The default constructor is used to provide the default values to the object
like 0, null, etc., depending on the type.

Example of parameterized constructor


In this example, we have created the constructor of Student class that have
two parameters. We can have any number of parameters in the constructor.

1. //Java Program to demonstrate the use of the parameterized constructor.


2. class Student4{
3. int id;
4. String name;
5. //creating a parameterized constructor
6. Student4(int i,String n){
7. id = i;
8. name = n;
9. }
10. //method to display the values
11. void display(){System.out.println(id+" "+name);}
12.
13. public static void main(String args[]){
14. //creating objects and passing values
15. Student4 s1 = new Student4(111,"Karan");
16. Student4 s2 = new Student4(222,"Aryan");
17. //calling method to display the values of object
18. s1.display();
19. s2.display();
20. }
21.}

Constructor Overloading in Java


In Java, a constructor is just like a method but without return type. It can also
be overloaded like Java methods.

Constructor overloading in Java is a technique of having more than one


constructor with different parameter lists. They are arranged in a way that
each constructor performs a different task. They are differentiated by the
compiler by the number of parameters in the list and their types.

Example of Constructor Overloading


1. //Java program to overload constructors
2. class Student5{
3. int id;
4. String name;
5. int age;
6. //creating two arg constructor
7. Student5(int i,String n){
8. id = i;
9. name = n;
10. }
11. //creating three arg constructor
12. Student5(int i,String n,int a){
13. id = i;
14. name = n;
15. age=a; }
16. void display(){System.out.println(id+" "+name+" "+age);}
17. public static void main(String args[]){
18. Student5 s1 = new Student5(111,"Karan");
19. Student5 s2 = new Student5(222,"Aryan",25);
20. s1.display();
21. s2.display();
22. }
23.}
this keyword in Java
There can be a lot of usage of Java this keyword. In Java, this is
a reference variable that refers to the current object.

Usage of Java this keyword


Here is given the 6 usage of java this keyword.

1. this can be used to refer current class instance variable.


2. this can be used to invoke current class method (implicitly)
3. this() can be used to invoke current class constructor.
4. this can be passed as an argument in the method call.
5. this can be passed as argument in the constructor call.
6. this can be used to return the current class instance from the method.
7. class Student{
8. int rollno;
9. String name;
10. float fee;
11. Student(int rollno,String name,float fee){
12. this.rollno=rollno;
13. this.name=name;
14. this.fee=fee;
15. }
16. void display(){System.out.println(rollno+" "+name+" "+fee);}
17. }
18.
19. class TestThis2{
20. public static void main(String args[]){
21. Student s1=new Student(111,"ankit",5000f);
22. Student s2=new Student(112,"sumit",6000f);
23. s1.display();
24. s2.display();
25. }}

Garbage collection in Java is the process by which Java programs perform


automatic memory management. Java programs compile to bytecode that can be
run on a Java Virtual Machine, or JVM for short. When Java programs run on the
JVM, objects are created on the heap, which is a portion of memory dedicated to
the program. Eventually, some objects will no longer be needed. The garbage
collector finds these unused objects and deletes them to free up memory.
Java garbage collection is an automatic process. Automatic garbage collection
is the process of looking at heap memory, identifying which objects are in use
and which are not, and deleting the unused objects. An in-use object, or a
referenced object, means that some part of your program still maintains a
pointer to that object. An unused or unreferenced object is no longer referenced
by any part of your program. So the memory used by an unreferenced object
can be reclaimed. The programmer does not need to mark objects to be deleted
explicitly. The garbage collection implementation lives in the JVM.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy