Java Classes and Objects

In Java, Class is what describes the properties and routines, an object could possess. In other words, “Class is a blueprint”.

Properties are variables in a class, that define the state of an object during its life time.

Routines are methods in a class, which define the behavior or tasks that an object can do.

In this tutorial, we shall learn about classes in Java, properties and methods of a class; and how to walk around creating objects and using them in Java programs.

Java Class Example

We see lot of objects in the real world. For example, Consider XY type of cars. There are lots of cars belonging to XY type which are analogous to objects. Since they have a blueprint, these XY cars are identical to each other. This blueprint, to which the manufacturers look up to, could be thought of a class in Java for understanding.

  • Properties could be current level of fuel, speed, head lights on/off, etc.,
  • Routines could be moving, breaking, etc.,

Following is an example of a class in Java.


public class CarClass {
	public final String carManufacturer = "XY";
	public int milesRun;
	public float velocity;
	public void run(){
		velocity = 20.0f; 
	public void applyBreak(){
		velocity = 0.0f;

Following is the detailed view of components in the class.

Java Classes and Objects - Java Class typical components - Tutorialkart
Java Class

How to create objects of a Java class?

The keyword, new, is used to create an object. Let us see how to create an object of class type CarClass that is defined above Java class example.

CarClass car1 = new CarClass();

Following picture depicts the components for declaring and creating a new object of class CarClass.

Java Classes and Objects - Class Object creation using "new" keyword - Tutorialkart
Class Object creation using “new” keyword

Access Properties and Call Methods of a Class Object

You can access properties and methods of a class using dot operator.

In the following example program, an object of type CarClass is created. Its variable milesRun is assigned a value using dot (.) operator. Methods of the object, run() and applyBreak() are called using dot(.) operator.


public class Traffic {

	public static void main(String[] args) {
		CarClass car1 = new CarClass();
		car1.milesRun = 0;;
		System.out.println("car1 is running..");
		System.out.println("Miles run by car1 : "+car1.milesRun);
		System.out.println("Velocity of car1 : "+car1.velocity);
		System.out.println("Applying break to car1..");
		System.out.println("Velocity of car1 after applying : "+car1.velocity);

Note : and are in same package, which is the reason for not importing the in

When the above program is run, the result is as shown in the following.

car1 is running..
Miles run by car1 : 1
Velocity of car1 : 20.0
Applying break to car1..
Velocity of car1 after applying : 0.0

This is how we define classes in Java and create object for a Class.


In this Java Tutorial, we learned how to define a Java Class; create objects for a Class; access properties and call methods on objects using dot operator; etc.

In our next tutorial, we shall learn about Inheritance in Java.