A class is a blueprint that tells Java how to create an object with specific properties and actions
In Java, a class is a template you write once and then use to create multiple objects that all work the same way. Think of a class as instructions for building a house: the class describes what rooms it has, what color the walls are, and what appliances go in each room. Once you write the class, you can use those instructions to build as many houses as you need, and each one will have the same structure.
Every class in Java lives in its own file with a name that matches the class name. If you create a class called Car, you save it in a file called Car.java. The class contains two main things: properties (also called fields or variables) that describe what the object is like, and methods (also called functions) that describe what the object can do.
Key Takeaways
- A class starts with the word public class followed by the class name, and the entire class body goes inside curly braces.
- Properties are variables declared inside the class that store information about each object, like a car's color or speed.
- Methods are actions the object can perform, written as functions inside the class that can read or change the object's properties.
- You create an actual object from a class by using the new keyword followed by the class name and parentheses, like new Car().
- Each object created from the same class is independent — changing one car's color does not change another car's color.
The basic structure of a class definition
Every Java class starts the same way. You type public class, then the name of your class (which must match the filename), then an opening curly brace. Everything that belongs to the class goes inside those braces, and you close it with a closing curly brace at the end.
Here is a straightforward class called Dog:
public class Dog { }
This class does nothing yet, but it is valid Java. To make it useful, you add properties and methods inside the braces. Properties go at the top, right after the opening brace. Each property needs a data type (like String for text, int for whole numbers, or boolean for true/false) and a name.
Adding properties to store information about each object
Properties are variables that belong to the class. Every object created from the class will have its own copy of these properties. If you create two Dog objects, each one can have a different name and a different age.
Properties are usually declared with the word private in front, which means only methods inside this class can read or change them. This protects the data from being changed in unexpected ways by code outside the class.
Here is the Dog class with properties added:
public class Dog { private String name; private int age; private String breed; }
Now each Dog object can store a name (text), an age (a number), and a breed (text). The properties exist, but they have no values yet — that happens when you create an object from the class.
Writing methods that let objects perform actions
Methods are functions that belong to the class. They describe what an object can do. A method can read the object's properties, change them, or perform some other action. Methods are written inside the class, after the properties.
A straightforward method looks like this:
public void bark() { System.out.println("Woof!"); }
This method is called bark, it returns nothing (that is what void means), and when you call it, it prints "Woof!" to the screen. The word public means any code outside the class can call this method.
Methods can also take parameters, which are values you pass in when you call the method. For example, a method that sets the dog's name might look like this:
public void setName(String newName) { name = newName; }
When you call setName("Buddy"), the method receives "Buddy" as the parameter newName and stores it in the property name. Methods can also return a value back to the code that called them. A method that returns the dog's age would look like this:
public int getAge() { return age; }
The int at the beginning tells Java that this method returns a whole number. When you call getAge(), it sends the value of age back to you.
Creating objects from your class using the new keyword
Once you have written a class, you create actual objects from it by using the new keyword. This is called instantiation — you are creating an instance of the class.
To create a Dog object, you write:
Dog myDog = new Dog();
This line creates a new Dog object and stores it in a variable called myDog. Now you can call the methods on that object:
myDog.setName("Buddy"); myDog.bark(); int dogAge = myDog.getAge();
You can create as many Dog objects as you want, and each one is separate. If you create another dog called anotherDog and set its name to "Max", the first dog's name stays "Buddy". Each object has its own copy of the properties.
Using constructors to set up objects when they are created
A constructor is a special method that runs automatically when you create a new object. It has the same name as the class and no return type. Constructors are useful for setting initial values for the object's properties.
Here is a constructor for the Dog class:
public Dog(String initialName, int initialAge) { name = initialName; age = initialAge; }
Now when you create a Dog, you pass in the name and age right away:
Dog myDog = new Dog("Buddy", 3);
The constructor runs when ready, sets the name to "Buddy" and the age to 3, and then the object is ready to use. If you do not write a constructor, Java provides a default one that does nothing, but writing your own lets you set up the object exactly how you want it.
A complete example class with properties, methods, and a constructor
Here is a complete Dog class that brings everything together:
public class Dog { private String name; private int age; private String breed; public Dog(String initialName, int initialAge, String initialBreed) { name = initialName; age = initialAge; breed = initialBreed; } public void bark() { System.out.println(name + " says Woof!"); } public void setName(String newName) { name = newName; } public String getName() { return name; } }
You can now create and use this class like this:
Dog myDog = new Dog("Buddy", 3, "Golden Retriever"); myDog.bark(); myDog.setName("Max"); System.out.println(myDog.getName());
The first line creates a dog named Buddy who is 3 years old and a Golden Retriever. The second line calls the bark() method, which prints "Buddy says Woof!". The third line changes the name to Max. The fourth line prints the new name to the screen.
Frequently Asked Questions
What is the difference between a class and an object?
A class is the blueprint or template — it describes what properties and methods objects of that type will have. An object is an actual thing created from that blueprint. The class Dog is the blueprint; myDog is the object.
Do I have to use the private keyword for properties?
No, but it is strongly recommended. Using private protects your properties from being changed in ways you did not expect. You can provide public methods like setName() and getName() to control exactly how the properties are read and changed.
Can a class have more than one constructor?
Yes. You can write multiple constructors with different parameters, and Java will use the one that matches the values you pass in when you create the object. For example, you could have one constructor that takes just a name, and another that takes a name and age.
What does the word void mean in a method?
It means the method does not return any value. When you call a method with void, it performs an action (like printing something or changing a property) but does not send anything back to you. Methods that return a value use a data type like int or String instead of void.
Can I use a class inside another class?
Yes. You can create objects from one class inside the methods of another class. For example, a Person class could create a Dog object and store it as a property, so each person can have their own dog.