You are on page 1of 5

Objects and Classes

 Object is the physical as well as logical entity whereas class is the logical entity
only.
 An entity that has state and behavior is known as an object e.g. chair, bike, pen,
table, car etc.
 Object has 2 characteristics:

o state: represents data (value) of an object.


o behavior: represents the behavior (functionality) of an object such as deposit,
withdraw etc.
o

Object Pen

Object State Color (White)

Object behavior writing is its behavior

How Objects are created


Class is a template or blueprint from which objects are created. So object is the
instance (result) of a class.
Every java program should have a class definition block.
While saving a java program the file name should be same as the class name.

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

A class in Java can contain:

o Fields/ data members


o Methods/ functions
o constructors
o blocks
o nested class and interface

Ex:

class <class_name>
{
field;
method1( );
method2( );

}
In java, a method is like function i.e. used to expose behavior of an object.

New keyword is used to allocate memory at run time.

Advantages of Method or function

o Code Reusability
o Code Optimization

Ex: Create a Student class with two data members’ id and name

Creating the object of the Student class by new keyword and printing the objects value.

Open Notepad and type the following code

Save the file as Student.java

class Student
{
int id; // data member or reference variable
String name;

public static void main (String args[ ])


{
Student s1=new Student (id, name); //creating an object of Student

System.out.println (s1.id); //accessing member through reference variable


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

Compile : javac Student.java

Run : java Student

Answer : 0
Null
In real time development, we create classes and use it from another class.

class Student // class name is student


{
int id; // reference variable
String name;

}
class TestStudent // another class
{
public static void main(String args[ ] )
{
Student s1=new Student( );
System.out.println(s1.id);
System.out.println(s1.name);
}
}

Compile : javac TestStudent.java

Run : java TestStudent

Result : 0

Null

There are 3 ways to initialize object in java.

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

class Student
{
int id; // reference Variable
String name;
}

class TestStudent2
{
public static void main(String args[])
{
Student s1=new Student( );
s1.id=001;
s1.name="USA”;
System.out.println(s1.id+" "+s1.name); //printing members with a white space
}
}

Compile : javac TestStudent2.java

Run : java TestStudent2

Result : 002 INDIA

How to create Multiple Objects and store information

TestStudent3.java

class student
{
int id; // reference variable
String name;
}
class TestStudent3
{
public static void main(String args[])
{
//Creating objects
Student s1=new Student( );
Student s2=new Student( );

//Initializing objects
s1.id=001;
s1.name="USA";
s2.id=91;
s2.name="INDIA";

//Printing data

System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
}

Compile : javac TestStudent3.java

Run : java TestStudent3

Result : 001 USA


91 INDIA

You might also like