Tutorial on Java Classes for Beginners

Classes in Java are one of the most important concepts in Java programming, and lies at the heart of all Java applications, toolkits, APIs and so on.

Sure, the number of declared classes do of course vary, but there’s no way around – every bit of logic defined in your application must reside within a Java class. (With the exception of a few statements, like imports and package declarations).

Theoretically, you can cram all your code within a single class, but as your code base grows bigger, you’ll probably benefit from separating your logic into classes.

What is a Java class?

A Java class is a single unit, which group related code together.

Classes can be seen as templates that are used to create objects, and will define the properties (variables) an object will contain, as well as various operations (methods).

Knowing that the above definition might make zero sense, let’s better our understanding by going through an example from the real world – before translating the example into Java code.

The example might seem silly at first – just bear with me 😉

Let’s assume you want to start your own business selling used cars.

For each car you want to sell, you want to list the following information:

  • Make.
  • Model.
  • Mileage.
  • Production year.
  • Price.

To make it easy for yourself, you decide to make a template for each advertisement, containing each of the 5 properties listed above.

Remember I mentioned that a class is basically a template to create objects?

Well, there you have it – your Car template, which contain properties that each and every car should have, is basically your class.

Let’s go ahead and define that class in Java.

Now, where do you put that code?
All top-level classes in Java must be placed in a file that has the exact name as the Java class, with the file type being .java.
Said in other words – place it in a file called Car.java 😉

What is a Java object?

A Java object is an entity that is created directly from a Java class.
An object, unlike a class, has it’s own state and identity.

Expanding on our previous car example, let’s assume you want to sell a car, with the following properties;

  • Make: Volvo
  • Model: S40
  • Mileage: 100000 miles
  • Production Year: 2004
  • Price: $4,000

Do you see how we used our template to create an entity (object) with it’s own state?

This specific car, the Volvo S40, is an object.
It has defined all the properties defined in our Car template (class), making it an object of type Car.

Now, let’s declare, instantiate and initiate our Volvo S40 object.

As you can see, there are three steps when creating an object from a class:

  • Declaration: Declaring a variable with a variable name, and an object type.
  • Instantiation: Using the ‘new’ keyword to create the object.
  • Initialization: Calling the constructor, which is a special method each class contains.

Regarding the constructor, in this example the call to the Car constructor is done on line number 7, Car().

If you don’t specify your own constructor, Java will automatically add one for you – which has the exact same name as the class.

I hope this article gave you a little more clarity to what a Java class actually is.

Happy coding!

Quick tip: If you wish to expand your Java skill-set and become an advanced Java developer, I highly recommend you to get yourself a copy of the best-selling Effective Java, by Joshua Bloch!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *