← Back to Core Java Course | Chapter 7: OOP Core | Lesson 2 of 11

Java Classes & Objects

A class is a blueprint, like a cookie cutter, and an object is one cookie made from it. Each object has its own details but follows the same design.
Syntax
java
class ClassName {
    dataType field;
    void methodName() {
        // body
    }
}

ClassName object = new ClassName();

Defining a Class

A class is a blueprint describing what data (fields) and behavior (methods) its objects will have, but no memory is actually allocated until you create an object from it — the class itself is just a template living in the JVM's metadata.

Example: Defining a Class

java
// Define the class Dog
class Dog {
	String name;
	int age;
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("Class defined, no object yet");
	}
}

Instantiating Objects

The new keyword instantiates an object, running the constructor and allocating memory for that specific instance on the heap. Every call to new produces a distinct object, even if you pass identical constructor arguments each time.

Example: Instantiating Objects

java
class Dog {
	String name;
}
public class Main {
	public static void main(String[] args) {
		Dog dog1 = new Dog();
		Dog dog2 = new Dog();
		System.out.println(dog1 == dog2); // false: distinct objects
	}
}

Working with Instance Methods

Instance methods operate on a specific object's data and require an object reference to be called (obj.method()), which is what lets them directly read and modify that particular object's fields without you passing the object explicitly as a parameter.

Example: Working with Instance Methods

java
// Define the class Dog
class Dog {
	// Declare name and set it to "Rex"
	String name = "Rex";
	void bark() {
		// Print a line to the console
		System.out.println(name + " says Woof!");
	}
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Dog object and store it in dog
		Dog dog = new Dog();
		dog.bark();
	}
}

Object State Independence

Every object created from a class gets its own independent copy of the class's instance fields, so changing one object's state has zero effect on any other object of the same class — this is the core reason objects are useful for modeling separate, independent entities.

Example: Object State Independence

java
// Define the class Dog
class Dog {
	String name;
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Dog object and store it in dog1
		Dog dog1 = new Dog();
		// Create a new Dog object and store it in dog2
		Dog dog2 = new Dog();
		dog1.name = "Rex";
		dog2.name = "Fido";
		// Print a line to the console
		System.out.println(dog1.name);
		// Print a line to the console
		System.out.println(dog2.name);
	}
}

Handling Null References

An object variable that hasn't been assigned (or has been explicitly set to null) doesn't point to any object at all; calling a method on it throws a NullPointerException at runtime.

Checking for null before dereferencing is one of the most common defensive patterns in Java code.

Example: Handling Null References

java
// Define the class Dog
class Dog {
	void bark() {
		// Print a line to the console
		System.out.println("Woof");
	}
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Dog dog = null;
		// Check whether dog != null
		if (dog != null) {
			dog.bark();
		} else {
			// Print a line to the console
			System.out.println("dog is null");
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting new when creating an object, so the variable is null.
  2. Calling a method on a null reference, which throws NullPointerException.
  3. Assuming two objects with the same data are ==, which compares references.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.