Objects are the entities that are used to carry real work in Java, or other programming languages.A typical Java program creates many objects, which as you know, interact by invoking methods. Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network. Consider the following example for a working example of objects

class Car {
	// data members
	int gear;
	int speed;
	int RPM;

	Car() // constructor Car called when car starts
		RPM = 1000; // some engine RPM when car starts
		System.out.println("Car Started, RPM is " + RPM);

	// Member functions
	void setGear(int newValue) { // set the gear to some value
		gear = newValue;
		System.out.println("Car is now in gear " + gear);

	void applyBrake(int decrement) { // break applying will decrease speed
		speed -= decrement;
		System.out.println("car's speed after applying break's is " + speed);

	void speedUp(int increment) { // pushing accelerator will speed up car
		speed += increment;
		System.out.println("Car's speed is " + speed);

public class CarClass {
	public static void main(String[] args) {
		Car car1 = new Car(); // car is started and RPM will be 1000;




		// let's slow our car


How Objects Are Created

Consider the line 32 of the above program. It says :

Car car1 = new Car();
first word : Car (is the name of Class of which the object is to be created)
second word : car1 (the object identifier)
third word : new (operator to initialize memory to the object car1)
fourth word : Car() (here we are calling the constructor of the class Car, it’s syntax, because memory initializations are done here internally, if we don’t declare a constructor above in class, then java goes with internal default constructor).

It’s also possible to assign one object to another as :


Car car1 = new Car();
Car car2 = car1;

In above both car1 and car2 will refer to same memory locations.


You may also like...