Java Objects and Classes Tutorial

In this tutorial, I will explain the fundamentals of java classes and objects.

Java is an object-oriented programming language. This means, that everything in Java, except the primitive types is an object. But what is an object at all? The concept of using classes and objects is to encapsulate state and behavior into a single programming unit. This concept is called encapsulation. Java objects are similar to real-world objects. For example, we can create a car object in Java, which will have properties like current speed and color; and behavior like: accelerate and park.

Creating a Class

Java classes are the blueprints of which objects are created. Let’s create a class that represents a car.

public class Car {
	
	int currentSpeed;
	String name;
	
	public void accelerate() {	
	}
	
	public void park() {
	}
	
	public void printCurrentSpeed() {
	}
}

Look at the code above. The car-object states (current speed and name) are stored into fields and the behavior of the object (accelerate and park) is shown via methods. In this example the methods are accelerate(), park() and printCurrentSpeed().

Let us implement some functionality into those methods.

1. We will add 10 miles per hour to the current speed of the car each time we call the accelerate method.

2. Calling the park method will set the current speed to zero

3. printCurrentSpeed method will display the speed of the car.




To implement these three requirements we will create a class named Car and store the file as Car.java

public class Car {
	
	int currentSpeed;
	String name;
	
	public Car(String name) {
		this.name = name;
	}
	
	public void accelerate() {
		// add 10 miles per hour to current speed
		currentSpeed = currentSpeed + 10;
	}
	
	public void park() {
		// set current speed to zero
		currentSpeed = 0;
	}
	
	public void printCurrentSpeed() {
		// display the current speed of this car
		System.out.println("The current speed of " + name + " is " + currentSpeed + " mpH");
	}
}

Class Names

When you create a java class you have to follow this rule: the file name and the name of the class must be equal. In our example – the Car class must be stored into a file named Car.java . Java is also case-sensitive: Car, written with capital C is not the same as car, written with lower-case c.

Java Class Constructor

Constructors are special methods. Those are called when we create a new instance of the object. In our example above the constructor is:

public Car(String name) {
	this.name = name;
}

Constructors must have the same name as the class itself. They may take parameters or not. The parameter in this example is “name”. We create a new car object using this constructor like this (I will explain this in more detail later in this tutorial):

Car audi = new Car("Audi");

Java Comments

Did you noticed the // marker in-front of lines 11, 16 and 21? This is how we write comments in Java. Lines marked as comment will be ignored while executing the program. You can write comments to give additional explanation of what is happening in your code. Writing comments is a good practice and will help others to understand your code. It will also help you when you come back later to your code.

Creating Objects

Now lets continue with our car example. We will create a second class named CarTest and store it into a file named CarTest.java

public class CarTest {
	
	public static void main(String[] args) {
		// create new Audi car
		Car audi = new Car("Audi");
		// create new Nissan car
		Car nissan = new Car("Nissan");
		
		// print current speed of Audi - it is 0 
		audi.printCurrentSpeed();
		
		// call the accelerate method twice on Audi
		audi.accelerate();
		audi.accelerate();
		
		// call the accelerate method once on Nissan
		nissan.accelerate();
		
		// print current speed of Audi - it is now 20 mpH
		audi.printCurrentSpeed();
		// print current speed of Nissan - it is 10 mpH
		nissan.printCurrentSpeed();
		
		// now park the Audi car
		audi.park();
		
		// print current speed of Audi - it is now 0, because the car is parked
		audi.printCurrentSpeed();
	}

}

In the code above we first create 2 new objects of type Car – Audi and Nissan. This are two separate instances of the class Car (two different objects) and when we call the methods of the Audi object this does not affect the Nissan object.

The result of executing CarTest will look like this:

The current speed of Audi is 0 mpH
The current speed of Audi is 20 mpH
The current speed of Nissan is 10 mpH
The current speed of Audi is 0 mpH

I encourage you to experiment with the code. Try to add a new method to the Car class or write a new class.

In our next tutorial you will learn more about the concepts of Object Oriented Programming.

5 2 votes
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments