Java Object and Class

Objects and Classes are the fundamental building blocks of OOPS (Object Oriented Programming). A java object is a physical and logical entity where as java class is logical entity. In this tutorial, we will learn about Java Objects and Classes.


We often read in our text books that "Object is an instance of a class". An object has a state and behavior. For example., a dog has states - color, name and behavior as well like barking, eating etc. 

The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created from templates known as classes. In Java, an object is created using the keyword "new".

Syntax for creating an object:

MyClass myObject = new MyClass();

In above example, 

  • MyClass is class name.
  • 'myObject ' is the object name (object name can be anything) 
  • 'new' is keyword of java which instantiates the object (in simple language it creates the object).
  • 'MyClass()'is the constructor of 'MyClass' which is called whenever an object is created.


Before creating an object in Java, you need to define a class. A class is a blueprint from which object is created. 

We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object. Since, many houses can be made from the same description, we can create many objects from a class. A class can have following entities inside it:

  • Fields (Variables)
  • Methods
  • Constructor
  • Nested Class
  • Interface
  • Blocks

Syntax to define a class:
public class Dog {

   String breed;
   int age;
   String color;

   public void barking() {

   public void hungry() {

Explanation of the example code:
  • Variables like breed, age and color are attributes or exhibits state.
  • Where as barking() and hungry() are methods which exhibits behavior.
Let's understand these concepts from an example:







Now, you can relate the definition that Class is a template and object is an instance of the class.

Click here to find complete Java Tutorials Series

Java Control Statements  << Previous     ||     Next >>  Java Constructor

Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.


Popular posts from this blog

10 Best Demo Websites for Automation Testing Practice

Automate E-Commerce Website using Selenium Webdriver

Automate Browser Actions on using Selenium

Automate Amazon Website 'Menu links' using Selenium