Create Class and Object in Java

Category: Java   Tags: Java, Java Basic, Java Class, Java Object

Here we are going to discuss Java classes and objects.

Classes in Java

A class is a blueprint or a template for creating objects. A Java objects exhibit the properties and behaviors defined by its class. To describe object behaviour, a class can contain fields and methods.

General Form Of a Java Class

                          class ClassName {
                            dataType instanceVariable1;
                            dataType instanceVariable2;
                            dataType instanceVariableN;

                            returnType methodName1() {


                            returnType methodName2(parameter1, .., parameterN) {


ClassName is the name given to a Java class. As per Java convention a class name should start with uppercase letter e.g Student, PlayerList are some valid Java class name.

dataType could be anything from Object, byte, short, int, long, char, float, double, boolean. Same is the case with returnType also. If a method returns nothing than it should be declared void. To know more about Java data types check out this page.

methodName is a name given to method. This can be any legal identifier. As per java standard, a valid method name could be like getEmployeeName, isEven and so on.

A method can accept parameter passed to it while calling it.

Objects in Java

An object is made of two thing: state and a behavior. Object's state is stored in fields and behavior is shown via methods. In case of our Student class all three fields: id, name, age are states and all set and get methods are behaviour of a Student object

An object is an instance of a class, and may be called a class instance or class object.

A Simple Java Example

                          package com.tutorial.javabasic;

                          class Student {
                              private String name;
                              private int id;
                              private int age;

                              public String getName() {
                                  return name;

                              public void setName(String name) {
                         = name;

                              public int getId() {
                                  return id;

                              public void setId(int id) {
                         = id;

                              public int getAge() {
                                  return age;

                              public void setAge(int age) {
                                  this.age = age;

Let's go through this program:

                          class Student {

This line say that this is a Java class whose name is Student. The class keyword is used to indicate a class in Java.

                          private String name;
                          private int id;
                          private int age;

Here we are declaring the instance variable of our Student class. name is a String variable, id is an integer variable, age is also an integer variable. To set and get the value of all these instance variable we have written setter and getter method.

A setter method start with set and a getter method start with get. Setter method is used to set a value and getter method is used to get the value of a variable.

Using a Java Class Methods

So we have our Student class, now this is the time to call this Java class. For this purpose we will create another Java class Main:

                          package com.tutorial.javabasic;

                          public class Main {
                              public static void main(String s[]) {
                                  Student student = new Student();
                                  student.setName("Abhay Kumar");

                                  System.out.println("Name: " + student.getName());
                                  System.out.println("Id: " + student.getId());
                                  System.out.println("Age: " + student.getAge());

Here we are creating a new object of Student type using Student student = new Student();. A method can be called using dot (.) operator on the instance name. setName(), setId(), setAge() methods are use to set name, id and age. Similarly getName(), getId(), getAge() methods are use to get name, id and age.

To get the basic idea of how a Java program is executed, read Write a Java Program.


To run this program execute the below command:

                          java Main

Running this command should show this on your console:

                          Name: Abhay Kumar
                          Id: 1
                          Age: 26


A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides.

A constructor is automatically called when the object is created and before the new operator completes. It has no return type, not even void. The implicit return type of a constructor is the class type itself.

It is the constructor’s job to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately. To know more about constructor check out this Java Constructor.

A constructor is used to initialize instance variale. Here is an example:

                          public class ConstructorExample {

                              private int a;
                              private int b;

                              public ConstructorExample() {
                                  a = 10;
                                  b = 20;

                              public ConstructorExample(int n1, int n2) {
                                  a = n1;
                                  b = n2;

                              private void multiply() {
                                  int result = a * b;
                                  System.out.println("a * b = " + result);
                              public static void main(String s[]) {
                                  ConstructorExample instance1 = new ConstructorExample();

                                  ConstructorExample instance2 = new ConstructorExample(20, 30);

Here there are two constructor: ConstructorExample() and ConstructorExample(int n1, int n2)

public ConstructorExample() is a default constructor. It will be called when ConstructorExample instance1 = new ConstructorExample(); is executed. A constructor without any parameter is called default constructor and it will be called every time an instance is created without any parameter.

public ConstructorExample(int n1, int n2) is called parameterized constructors as it accept parameter n1 and n2. This will be called when ConstructorExample instance2 = new ConstructorExample(20, 30); is executed.