Professional Documents
Culture Documents
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:
Object Pen
Class
A class is a group of objects which have common properties.
Ex:
class <class_name>
{
field;
method1( );
method2( );
}
In java, a method is like function i.e. used to expose behavior of an object.
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.
class Student
{
int id; // data member or reference variable
String name;
Answer : 0
Null
In real time development, we create classes and use it from another class.
}
class TestStudent // another class
{
public static void main(String args[ ] )
{
Student s1=new Student( );
System.out.println(s1.id);
System.out.println(s1.name);
}
}
Result : 0
Null
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
}
}
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);
}
}