Java is an Object-Oriented (OO) language. As such it represents real world entities – both living and inanimate – as objects. For example, a car is an object, as is your Grandmother (sorry grandma). All Java objects possess attributes, such as weight and color, and methods, such as drive, brake, and even knit. So what does all this have to do with classes? A Java class is like an object constructor, or a “blueprint” for creating objects. So, before we create an object, we first need to define the class. Once defined, we can create many similar objects using the same class. In this programming tutorial, we will learn about the concept of classes and objects in Java with the help of many code examples.
Looking to learn Java programming in an online class environment? Check out our guide to the Best Online Courses to Learn Java.
How to Create a Class in Java
Developers can create a class in Java using the class keyword:
class ClassName { // fields // methods }
Here is a class definition for a class named “Main” with a variable myInt:
class Main { int myInt = 15; }
How to Create an Object in Java
Having defined our class, we can now use it to create objects. To create a Person object, for example, we need to specify the class name, followed by the object name, and use the new keyword:
Person person = new Person();
The main() Method in Java
Similar to the the main() methods of C and C++, the main() method in Java is executed by the Java Virtual Machine (JVM) when it starts our program. Hence, it is the program’s starting point. Programmers can build a complete Java program simply by including it in our Person class, as shown in the following code:
public class Person { int age = 15; public static void main(String[] args) { Person person = new Person(); System.out.println(person.age); // 15 } }
You might think that the JVM would have to create an Object to run the program, but it does not, because the main method is static. Hence, no object is created until the Person fred = new Person();
line.
We can reuse the same class definition to create more Person objects:
public class Person { int age = 50; public static void main(String[] args) { Person fred = new Person(); System.out.println(fred.age); // 50 Person wilma = new Person(); System.out.println(wilma.age); // 50 } }
Since the age variable is initialized with a value of 50, both our objects display the same value. It would be useful if they could each have their own value. In fact, there are a couple of ways to do that. The first, which we will see next, is to pass in the value to the constructor.
Java Class Constructors
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. The constructor name must match the class name, and it cannot have a return type (not even void). Constructors can, however, accept parameters. Here is an example of a Java constructor that accepts a value for the age variable:
public class Person { // no initial value necessary // because it will be overwritten in the constructor int age; public Person(int anInt) { age = anInt; } public static void main(String[] args) { Person fred = new Person(50); System.out.println(fred.myInt); // 50 Person wilma = new Person(39); System.out.println(wilma. myInt); // 39 } }
The Default Constructor
For convenience, no constructor is necessary to create an object. That is because Java includes a default no-argument constructor automatically. Therefore, developers do not need to include a constructor unless we want to pass in some values as we did above. However, once we include a constructor, the default one goes away. In fact, a common mistake is to leave default constructor invocations in the code after introducing one that accepts parameters.
Here is our Main class again, this time with the addition of an explicit no-argument constructor that assigns a default value to the age variable:
public class Person { int age; // Replaces the default constructor public Person() { // Now 15 will be the default value age = 15; } public Person(int anInt) { age = anInt; } public static void main(String[] args) { Person fred = new Person(50); System.out.println(fred.age); // 50 Person wilma = new Person(39); System.out.println(wilma.age); // 39 } }
You can learn more about Java constructors in our tutorial: Overview of Constructors in Java.
Java Class Attributes
As we saw in the code example above, the age variable is an attribute of the Main class. After instantiating a person from our Person class, we were able to access age’s value using the dot syntax (i.e. fred.age).
We can also use dot syntax to update a person’s age:
public class Person { int age = 45; public static void main(String[] args) { Person barney = new Person(); System.out.println(barney.age); // 45 barney.age = 46; System.out.println(barney.age) ; // 46 } }
How to Work with Multiple Classes in Java
Most Java applications contain many classes. Although developers may define multiple classes within a single source file, it is common practice to save each class to its own file. In that case, the file name must consist of the class name and the .java extension.
Here is a Java program consisting of two classes, one for the Person entity, and the other for the MultipleClassesExample application:
// Person.java file class Person{ int age; String name; } // MultipleClassesExample.java // Class that contains the main method class MultipleClassesExample { public static void main(String args[]){ Person betty = new Person(); betty.age = 36; betty.name = "Betty"; System.out.println(betty.age); // 55 System.out.println(betty.name); // Betty } }
Going Forward
In this programming tutorial, we learned about the concept of classes and objects in Java with the help of many examples. However, we are far from done; in the next few weeks, we will be updating this tutorial as we cover the this pointer, accessors and mutators, inheritance, and more OOP concepts!