Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Java
  • Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
  • Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
  • Java Hello World with Maven: What the JDK Does and What Maven Adds
  • Java Variables, Keywords and Naming Rules
  • Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
  • Java Primitive Types and Memory: What int x = 5 Really Does
  • Java Strings and Text Blocks: Quotes, Concatenation and parseInt
  • Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary
  • Java Type Casting: Widening, Narrowing and Converting Text to Numbers
  • Java Practice Lab: A Cafe Bill with Variables, Operators and Casting
  • Java if, else and switch: Braces, Else-If Ladders and the Modern Switch
  • Java Loops: for, while, do-while and for-each
  • Java break, continue and Nested Loops
  • What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
  • Java Arrays: Indexes, Default Values, Loops and 2D Grids
  • Java Methods and Overloading: Parameters, return and Pass-by-Value
  • Java Scope, Code Blocks and the Call Stack
  • Read User Input in Java with Scanner (and the Leftover Enter Trap)
  • Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
  • Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
  • Java var: Let Java Infer the Type
  • Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
  • Java Classes and Objects: One Blueprint, Many Cars
  • Java Constructors and the this Keyword
  • Java Instance Methods, Getters, Setters and toString
  • Java static: Fields, Methods and Blocks Shared by the Whole Class
  • Java Packages and Imports: Every Class Has an Address
  • Java Access Modifiers and Encapsulation: Private Data, Public Methods
  • Java final and Immutable Objects: Assign Once, Share Safely
  • Java Strings as Objects: The String Pool, == vs equals, StringBuilder and printf
  • Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
  • Java Inheritance: extends, super and the Order Objects Are Built
  • Java Method Overriding, @Override and the Object Class
  • Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
  • Java Composition: Has-A vs Is-A
  • Java Abstract Classes: A Parent That Leaves One Blank
  • Java Interfaces: Contracts and Multiple Inheritance
  • Java Nested, Inner and Anonymous Classes
  • Java Enums with Fields, Methods and switch
  • Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
  • Java Records: A Whole Data Class in One Line
  • Java Sealed Classes and Interfaces
  • Java Pattern Matching with instanceof and switch
  • Java Record Patterns: Unpacking a Record
  • Java equals and hashCode: Why a HashSet Needs Both
  • From Arrays to Collections: The Java Collections Framework Map
  • Java Wrapper Classes and Autoboxing
  • Java ArrayList: The Stretchy Array
  • Java LinkedList vs ArrayList: Nodes or an Array
  • Java Sets: HashSet, LinkedHashSet and TreeSet
  • Java Sorting with Comparable and Comparator
  • Java Maps: HashMap, LinkedHashMap and TreeMap
  • Java Stacks, Queues and Deques: ArrayDeque and PriorityQueue
  • Choosing a Java Collection: Immutable and Sequenced Collections
  • Java Exception Handling: try, catch and finally
  • Java throw, throws, Checked vs Unchecked and Custom Exceptions
  • Java try-with-resources and Clean Error Handling
  • Java Generics: Type-Safe Containers
  • Java Generic Methods, Bounded Types and Wildcards
  • Java Lambdas and Functional Interfaces
  • Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
  • Java Optional: Saying Maybe Without null
  • Java Streams I: filter, map and reduce
  • Java Streams II: Collectors, groupingBy and flatMap
  • Why Concurrency: Processes, Threads and the Main Thread in Java
  • Creating Threads in Java: Thread, Runnable and Lambdas
  • Java Thread Lifecycle: sleep, join and interrupt
  • Java Race Conditions and synchronized
  • Java volatile and Atomic Variables
  • Java ExecutorService and Thread Pools
  • Java Callable, Future and CompletableFuture
  • Java Locks, Deadlock and tryLock
  • Java Concurrent Collections and Virtual Threads
  • Why Slow Work Belongs off the Request Thread: 202 Accepted and a Job Id in Spring Boot
  • Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
  • @Async, Named TaskExecutors and Structured Concurrency in Spring Boot
  • Java Files and Directories with NIO.2: Path and Files
  • Reading and Writing Text Files in Java
  • Java Structured Data: Reading and Writing JSON and CSV
  1. Blog
  2. /
  3. Java
  4. /
  5. Object-Oriented Programming

Java Classes and Objects: One Blueprint, Many Cars

Six loose variables for two cars doesn't scale. A Car class is the blueprint; new builds each car with its own brand, speed and fuel. Plus field defaults, and why two variables can point at one car.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Object-Oriented Programming

What you will learn

  • Explain a class as a blueprint and an object as the thing built from it
  • Create objects with new and set and read fields with the dot
  • Know the default values of fields: null, 0, 0.0 and false
  • Explain why Car b = a copies the reference, not the object

Before you start

  • Java variables, keywords and naming
  • Java primitive types and memory

Say you want to store two cars. With loose variables that's taxiBrand, taxiSpeed, taxiFuel, then sedanBrand, sedanSpeed, sedanFuel: six variables for two cars, and 150 for fifty. Worse, nothing tells Java that taxiSpeed belongs to the taxi.

A class fixes this. It's a blueprint, like an architect's plan for a house: you can't live in the plan, but it describes the rooms and doors. From one blueprint you build many real houses. Those are objects.

The Car class is a blueprint listing brand, speed and fuel. Two objects are built from it with new: taxi, a Toyota at 60, and sedan, a Honda at 100. Each object has its own fields. class Car (blueprint) String brand int speed double fuel taxi (object) Toyota, 60 km/h sedan (object) Honda, 100 km/h new Car() new Car() written once, used many times
One class, many objects, each with its own field values.

Describe a car once

package org.studyeasy.s03l01;

// The blueprint: every Car object gets its own copy of these three fields
public class Car {
    String brand;
    int speed;
    double fuel;
}

public class Car creates a new type in its own file, Car.java. It describes a car, but no car exists yet. The three variables inside are fields: the data every car carries. A class body holds declarations, not statements: write brand = "Toyota"; directly in it, outside any method, and javac refuses.

new builds one car

package org.studyeasy.s03l01;

public class Ex01_FirstCar {
    public static void main(String[] args) {
        Car taxi = new Car();          // build one Car from the blueprint
        taxi.brand = "Toyota";         // fill in its own fields
        taxi.speed = 60;
        taxi.fuel = 42.5;

        System.out.println("Brand: " + taxi.brand);
        System.out.println("Speed: " + taxi.speed + " km/h");
        System.out.println("Fuel:  " + taxi.fuel + " L");
    }
}
Brand: Toyota
Speed: 60 km/h
Fuel:  42.5 L

new builds the object; taxi is the variable that holds it. The dot sets a field (taxi.brand = "Toyota") and reads it back (taxi.brand). Leave out = new Car() and javac says the variable might not have been initialized: there's no car to fill.

Two cars, two sets of fields

package org.studyeasy.s03l01;

public class Ex02_TwoCars {
    public static void main(String[] args) {
        Car taxi = new Car();
        taxi.brand = "Toyota";
        taxi.speed = 60;

        Car sedan = new Car();         // a second car, its own fields
        sedan.brand = "Honda";
        sedan.speed = 100;             // the taxi stays at 60

        System.out.println(taxi.brand + ": " + taxi.speed + " km/h");
        System.out.println(sedan.brand + ": " + sedan.speed + " km/h");
    }
}
Toyota: 60 km/h
Honda: 100 km/h

Another new, another car. Same class, separate fields, so setting the sedan to 100 leaves the taxi at 60. Car lives in its own file and this program just uses it. A class in a different package would need an import (see packages and imports).

Fields start with default values

package org.studyeasy.s03l01;

public class Ex03_DefaultValues {
    public static void main(String[] args) {
        Car spare = new Car();         // no field assigned yet
        System.out.println("brand = " + spare.brand);
        System.out.println("speed = " + spare.speed);
        System.out.println("fuel  = " + spare.fuel);
    }
}
brand = null
speed = 0
fuel  = 0.0
Field typeDefault
String (and any other object)null
int0
double0.0
booleanfalse

null means "nothing there yet". It isn't an empty string. Use it, for example with spare.brand.length(), and Java throws a NullPointerException: there's no text to measure.

Two variables, one car

package org.studyeasy.s03l01;

public class Ex04_TwoReferences {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.brand = "Toyota";
        myCar.speed = 60;

        Car emmasCar = myCar;          // same car, no new one
        emmasCar.speed = 90;           // myCar sees 90 too

        System.out.println("myCar.speed    = " + myCar.speed);
        System.out.println("emmasCar.speed = " + emmasCar.speed);
        System.out.println("same car? " + (myCar == emmasCar));
    }
}
myCar.speed    = 90
emmasCar.speed = 90
same car? true
Two variables, myCar and emmasCar, both hold a reference that points to one Car object: Toyota with speed 90. myCar emmasCar Car object Toyota, speed 60 → 90
= copies the reference, never the car. Two names, one object.

myCar doesn't hold the car itself; it holds a reference to it, and the object lives on its own. Car emmasCar = myCar; builds nothing. It copies the reference, so both variables point at the same object. Change the speed through one and the other sees 90 too. == on objects asks "is it the very same object?", and the answer is true. Change = myCar to = new Car() and you get two cars, different speeds, and false.

Recap

IdeaRemember
ClassA blueprint, written once
ObjectBuilt with new; each has its own field values
FieldsVariables in the class; set and read with the dot
Defaultsnull, 0, 0.0, false
Car b = a;Copies the reference: one object, two names

Setting three fields by hand after every new gets old fast. Constructors build a car that's ready from the first line.

Keep going in Java · 23 of 79

Previous
Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
Next post
Java Constructors and the this Keyword
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Related posts

Java Constructors and the this Keyword

A constructor decides how an object is born: new Car("Toyota", 42.5) arrives filled in. this.brand vs brand, a second constructor that calls this(brand, 50.0), and why new Car() stops compiling once you write your own.

Java Instance Methods, Getters, Setters and toString

Give a Car class behaviour: accelerate() changes only this car's speed, private fields are reached through getSpeed and setSpeed, and toString turns Car@1b6d3586 into 'Toyota doing 60 km/h'.

Java static: Fields, Methods and Blocks Shared by the Whole Class

A static field lives once on the class: every Car ticks the same carsBuilt counter while keeping its own brand. Static methods need no object, a static block runs before main, and main is static so the JVM can start with no object.

Java Packages and Imports: Every Class Has an Address

Two classes called Order, one in shop and one in shipping: the package keeps them apart. How the package line maps to folders, plain and static imports, using both Orders in one file, and the clash when you import both.

On this page

  • Describe a car once
  • new builds one car
  • Two cars, two sets of fields
  • Fields start with default values
  • Two variables, one car
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.