Java classes/objects
Java objects
Java objects
An object has three characteristics:
• State: represents the data (value) of an
object.
• Behavior: represents the behavior
(functionality) of an object such as
deposit, withdraw, etc.
• 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.
Java objects
• Java is an object-oriented programming language.
• Everything in Java is associated with classes and objects.
• An object is any entity that has a state and behavior.
• For example: in real life, a car is an object.
• The car has attributes, such as weight and color, and methods, such as drive and
brake.
• The object is an instance of a class.
Java 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:
• Fields
• Methods
• Constructors
• Blocks
• Nested class and interface
Java Class
• A class is a blueprint for the object.
• Before we create an object, we first need to define the class.
• We can think of the class as a sketch (prototype) of a house.
• It contains all the details about the floors, doors, windows, etc.
• Based on these descriptions we build the house.
• House is the object.
Java Class Syntax
class ClassName
// fields
// methods
• fields are used to store data
• methods are used to perform some operations
A class in Java can contain:
• Fields
• Methods
• Constructors
• Blocks
• Nested class and interface
• For bicycle object, we can create the class as:
class Bicycle {
// state or field
int gear = 5;
// behavior or method
public void braking() {
System.out.println("Working of Braking");
}
Java Objects
• An object is called an instance of a class.
• For example, suppose Bicycle is a class then MountainBicycle, SportsBicycle,
TouringBicycle, etc can be considered as objects of the class.
Creating an Object in Java
• Java Object Syntax: className variable_name = new className();
// for Bicycle class
• Bicycle sportsBicycle = new Bicycle();
• Bicycle touringBicycle = new Bicycle();
• className is the name of class that can be anything like: Bicycle that we
declared in the above example.
• variable_name is name of reference variable that is used to hold the reference of
created object.
• The new is a keyword which is used to allocate memory for the object.
Example: Creating a Class and its object
public class Student{
String name;
int rollno;
int age;
void info(){
System.out.println("Name: "+name);
System.out.println("Roll Number: "+rollno);
System.out.println("Age: "+age);
}
public static void main(String[] args) {
Student student = new Student();
// Accessing and property value
student.name = "Ramesh";
student.rollno = 253;
student.age = 25;
// Calling method
student.info();
Output:
Name: Ramesh
Roll Number: 253
Age: 25
3 ways to initialize object
There are 3 ways to initialize object in Java.
• By reference variable
• By method
• By constructor
1) Object and Class Example: Initialization through reference
• Initializing an object means storing data into the object.
Example
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);
} }
We can also create multiple objects and store information in it through
reference variable.
class Student{
int id;
String name;
}
class TestStudent3{
public static void main(String args[]){
//Creating objects
Student s1=new Student();
Student s2=new Student();
//Initializing objects
s1.id=101;
s1.name="Sonoo";
s2.id=102;
s2.name="Amit";
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
}
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.
class Student{
int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void displayInformation(){System.out.println(rollno+" "+name);}
}
class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
• Object gets the memory in heap memory area.
• The reference variable refers to the object allocated in the heap memory area.
• Here, s1 and s2 both are reference variables that refer to the objects allocated in memory.
3) Object and Class Example: Initialization through a constructor
• We will learn about constructors in Java later.
What are the different ways to create an object in Java?
There are many ways to create an
object in java. They are:
• By new keyword
• By newInstance() method
• By clone() method
• By deserialization
• By factory method etc.