Master java skills

Objects in Java

Object is a real thing. It is an instance of a class which has a state (meaning it has values of attributes). At different points in time, an object can have different states (meaning different values of its properties). Objects have states and behaviour. A woman named as “Meera” is an object of Woman class. Similarly, a real donkey is an object of Donkey class and a cup of actual coffee is an object of coffee class. Following points clearly define an object:

  • An object is an instance of a class
  • An object is a real thing (logical, such as circle is an object of Shape class; or actual, an actual car is an object of Car class)
  • An object has state and behaviour
  • An object is a runtime entity
Example of objects

Most commonly, objects can be created in the following way

<class-name> <object-name> = new <class-name> ();

examples :

Doctor d1 = new Doctor();
Tiger t1 = new Tiger();
Writer writer = new Writer();
Painter mark = new Painter();
Novelist Saras = new Novelist();
public class Coffee {
	
	//class attributes
	private String name;
	private String colour;
	private String type;
	
	public void energizeConsumer() {
		System.out.println("The consumer is energized.");
	}
	
	public static void main(String[] args) {
		
		Coffee c1 = new Coffee();
		
		c1.name = "Mocha";
		c1.colour = "Light Brown";
		c1.type = "Hot Beverage";
				
		c1.energizeConsumer();
		
		System.out.println("The name of c1 : " + c1.name);
		System.out.println("The colour of c1 : " + c1.colour);
		System.out.println("The type of c1 : " + c1.type);
	}
}

Output :

The consumer is energized.
The name of c1 : Mocha
The colour of c1 : Light Brown
The type of c1 : Hot Beverage