Java Object


Objects are key to understanding object-oriented technology. Look around right now and you’ll find many examples of real-world objects: your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming. objects are used to access the implementation of class, means you can access the class data member and data fuction or method.
Declaration of object

To declare a object for a class we write the syntax : class name object name = new class constructor call.  e.g. Student s = new Student(); Here

  • Student is class.
  • S is object of class Student.
  • New is keyword use to creates a Java object and allocates memory for it on the heap. new is also used for array creation, as arrays are also objects.
  • Student(); is a constructor, A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations
Demonstration of  java object
class AddNumber
int x=4;
int y=6;
int sum(){
int sum;
System.out.println(“value of x and y within function is :”+x+” and “+y);
return sum;
public static void main(String[] args)
AddNumber add =new AddNumber();
System.out.println(“value of x in main using object”+b.x);
int z=add.sum();
System.out.println(“value of z is “+z);

output :value of x in main using object 4
value of x and y within function is :4 and 6
value of z is 10

AddNumber is a class.
x and y is data members or variables of the class AddNumber, storing values 4 and 6 respectively.
we can use these variables’s values inside the class function or method .that is sum().
int sum is variable of method sum , we can’t use the value of sum in another method of same class.
public static void main(String[] args) is main program.inside the main fuction we declare the object of AddNumber class.
add is object of the class AddNumber. if we want to access the variable of class AddNumber in the main fuction the we can call them using add object.
suppose if you want to access the value of x variable, then you need to call them using add object. e.g add.x; dot must be required between object and varible.
if you want to invoke the method of class AddNumber then you can call them .syntax is e.g : add.sum();
int z storing the value of method sum().
+ is used to print the value inside the println() method

Program to print the student name and roll no by using object
public class Student {
String name=”Devil”;
int rno=526;
public static void main(String[] args){
Student s =new Student();
System.out.println(“student name is :”;
System.out.println(“student roll no is :”+s.rno);

output :student name is :Devil
student roll no is :526