If you cannot model the real world in your code, you will end up writing hundreds of disorganised, repetitive instructions that are impossible to fix when something breaks — and something always breaks. Object-oriented programming (OOP) solves this mess by letting you create blueprints (classes) and then stamp out working copies (objects) that each have their own data and abilities. For the Oracle Java Foundations 1Z0-811 exam, mastering classes and objects is the single most important skill, because every Java programme you ever write will rely on them.
Jump to a section
A simple way to picture Object-Oriented Programming: Classes and Objects
When you walk into a bakery and order a cake, you start with a blueprint — a recipe. The bakery has one master recipe for a 'chocolate birthday cake' that lists all the ingredients (flour, eggs, cocoa, sugar) and the steps to combine them. This master recipe is like a class. It describes what every chocolate birthday cake will have, but it is not itself a cake. You cannot eat a recipe. For your actual birthday party, the baker uses that recipe to create 3 separate cakes — one for the office party, one for your family dinner, and one for your friends’ gathering. Each of those 30-cm round, double-layer chocolate cakes with buttercream icing is an object. Each object is an individual instance of the recipe that the baker made. Each cake has its own specific decorations — the office cake might have 'Happy Birthday' written in blue icing while the family cake has pink roses. Those decorations are the instance variables. Each cake also knows how to respond to a command like 'cut a slice' or 'add a candle', which are the methods. The recipe (the class) defines the method 'cut a slice' once, and every cake (every object) made from that recipe can carry out that action. The recipe is just a plan. The cake is the real thing you can hold, eat, and even drop on the floor. A single class can create hundreds of objects, just like a single recipe can produce hundreds of cakes. Each object exists separately and independently in memory, just as each cake sits on its own plate at different parties.
Object-oriented programming (OOP) is a way of organising your code so that it mirrors how humans naturally think about the world. Instead of writing one giant list of commands, you create small, self-contained units called classes. A class is a blueprint — it defines what something is and what it can do. For example, if you wrote a class called 'Dog', that class would define that every Dog has a name, an age, and a breed (these are called instance variables). It would also define that every Dog can bark, run, and eat (these are called methods). A class is just a plan on paper. The real thing you work with in your programme is an object. An object is a concrete, living instance of a class. When you write 'new Dog()', you are telling Java to take the blueprint and build a real Dog object in the computer's memory. That Dog object will have its own name, its own age, and its own breed, separate from any other Dog object.
To create a class, you start with the keyword 'class' followed by the name you want to give your blueprint. Class names in Java always start with a capital letter by convention. Inside the curly braces of the class, you declare instance variables — these are also called fields or attributes. An instance variable is a piece of data that belongs to a specific object. For a class called 'Student', instance variables might include String studentName, int studentId, and double gpa. Note that instance variables are declared directly inside the class, not inside a method. Each object of the Student class will have its own copy of these variables. If you create two Student objects, changing the studentName of one will not affect the other.
To create an object from a class, you use the 'new' keyword followed by the class name and empty parentheses. This is called instantiation. For example, Student s1 = new Student();. The variable 's1' now holds a memory address that points to the newly created Student object. The 'new' keyword allocates memory for the object and calls a special method called a constructor to initialise the instance variables. If you do not write your own constructor, Java gives you a default one that sets numeric variables to 0 and object references to null.
Once you have an object, you can access its instance variables and call its methods using the dot operator. The dot operator is exactly what it sounds like — a single dot placed between the object reference and the variable or method name. For example, s1.studentName = "Alice"; or s1.calculateGpa();. This operator tells Java to look inside the object that 's1' points to and find the piece of data or behaviour you want.
Methods are blocks of code that define a behaviour for the object. A method declaration includes a return type (what kind of value the method sends back), a name, parentheses for any parameters, and a body in curly braces. A method with return type 'void' sends nothing back. A method with return type 'int' sends back an integer. To call a method, you use the object reference, a dot, the method name, and any arguments in parentheses.
Instance variables and methods have visibility modifiers like 'public' and 'private'. For 1Z0-811, you mostly use 'public', which means any other code can see and use them. Instance variables should ideally be 'private' with public methods to access them (this is called encapsulation), but the exam focuses on the basic mechanics.
The reason OOP exists is that it solves the problem of spaghetti code — long, tangled scripts where changing one thing breaks everything. By bundling data and behaviour together in classes, you create reusable, modular components. You can write a class once and create hundreds of objects from it. You can also change the class blueprint later, and all new objects will automatically reflect the changes. This is why nearly all modern programming languages, including Java, are object-oriented.
1. Define the class blueprint
Write the 'class' keyword, then a class name starting with a capital letter, then open and close curly braces. Inside the braces, you will later add instance variables and methods. This step creates the design on paper.
2. Declare instance variables
Inside the class curly braces, outside any method, declare variables that represent the state of each object. For a Car class, you might declare 'String colour;' and 'int speed;'. Each instance of Car will have its own colour and speed.
3. Write one or more methods
Inside the class, write methods that define behaviours. A method has a return type, a name, parentheses for parameters, and a body. For a Car, you could write 'void accelerate() { speed = speed + 10; }'. Methods act on the instance variables of the specific object.
4. Create a reference variable
In your main method or elsewhere, write the class name followed by a variable name, like 'Car myCar;'. This creates a variable that can hold the memory address of a Car object, but no object exists yet.
5. Instantiate the object with 'new'
Write 'myCar = new Car();' or combine it as 'Car myCar = new Car();'. The 'new' keyword allocates memory for a Car object, calls the constructor, and returns the memory address. Now myCar points to a real object.
6. Use the dot operator to interact with the object
To set the colour of your car, write 'myCar.colour = "Red";'. To make it accelerate, write 'myCar.accelerate();'. The dot operator tells Java to look inside the object myCar points to and find the variable or method you named.
Imagine you are working for a small online bookstore. Your manager asks you to write a programme that manages all the books in the warehouse. Without OOP, you would have to write separate variables for every single book — book1Title, book1Author, book1Price, book1Stock, book2Title, book2Author, book2Price, book2Stock, and so on. If you have 10,000 books, that is 40,000 separate variables and your code becomes a nightmare to maintain. With OOP, you write one class called 'Book'.
You start by defining the class with three instance variables: String title, String author, double price, and int stockQuantity. You also write a method called 'restock(int quantity)' that increases the stockQuantity, and another method called 'sell(int quantity)' that decreases it (and checks if you have enough stock first). You write one method called 'printDetails()' that prints the title, author, and price nicely.
Now, when new books arrive from the publisher, you loop through a spreadsheet and for each row, you call: Book b = new Book(); then set b.title = the title from the spreadsheet, b.author = the author, etc. Each call to 'new' creates a new Book object in memory. You store all these Book objects in a data structure like an array or a list. You now have a clean, organised system.
Later, when a customer buys a book, you find the correct Book object in your collection and call b.sell(1). The method checks if there is enough stock and updates the stock count. If the publisher sends a restock, you call b.restock(50). Every action is safe and contained within the object. If you later need to add a new feature — like an ISBN number — you just add a new instance variable to the Book class, and all existing Book objects automatically have space for it (even though their value starts as null).
What does an IT professional do with this? Every day, developers model real-world entities as classes: Customer, Order, Invoice, Payment, Product, ShoppingCart. They instantiate objects to represent each real entity in the system. They call methods on those objects to process payments, send emails, update databases, and generate reports. The entire Spring framework (a popular Java framework) is built on the idea of objects that the framework creates and manages for you. When you write an Android app, you create objects for buttons, text fields, and images. When you build a web app, you create objects for user sessions and HTTP requests. Classes and objects are not just exam topics — they are the literal building blocks of every Java programme in production.
The 1Z0-811 exam tests classes and objects in a very direct, no-nonsense way. The exam does not ask you to write a full programme from scratch. Instead, it shows you short code snippets and asks you to predict the output, spot errors, or identify which line of code correctly creates an object. About 15-20% of the exam questions touch on this objective.
Here are the exact concepts the exam loves to test:
Creating a class: The syntax must start with 'class', then the class name (capital letter), then opening and closing curly braces. The exam will show you code where the class name is misspelled or the keyword is missing, and you need to spot the mistake.
Creating an object with 'new': The exam tests that you must use the 'new' keyword followed by the class name and parentheses. Common traps include writing 'Dog myDog;' without the 'new' keyword (which only declares a reference variable, not an actual object) or writing 'new Dog;' without parentheses.
Accessing instance variables and methods: The syntax is objectReference.variableName or objectReference.methodName(). The exam will sometimes show you code where the dot is missing or the object reference is null, causing a NullPointerException.
Understanding the difference between a class and an object: The exam will ask questions like 'Which of the following is a valid declaration of a class?' and then give you options where one is an object instantiation. You must recognise that 'class Car { }' defines a class, while 'new Car()' creates an object.
Default values: The exam tests that instance variables of type int default to 0, double defaults to 0.0, boolean defaults to false, and object references (like String) default to null. A question might show you a class with instance variables, instantiate the object, and then print the variable without setting it, asking for the output.
Multiple objects from the same class: The exam will create two objects from the same class, change an instance variable on one, and ask whether the other object sees the change. The answer is always no — each object has its own separate copy.
The traps the exam sets include:
Confusing a reference variable declaration with object creation: 'MyClass obj;' only creates a placeholder; no object exists yet.
Forgetting that methods with a non-void return type must return a value: the exam will show a method that says 'int calculate() { }' with no return statement, and it will not compile.
Mixing up parameter names with instance variable names: if a method parameter has the same name as an instance variable, you need to use the 'this' keyword to refer to the instance variable. The exam does not require deep 'this' usage, but it appears in some questions.
Assuming a class can only have one object: the exam tests that you can create many objects from one class.
The key to exam success is to practise reading code line by line. For each question, identify whether the line declares a class, instantiates an object, accesses a variable, or calls a method. If you see 'new', an object is being born. If you see a class name with no 'new', it is either a class declaration or a variable declaration.
A class is a blueprint written in code that defines what data and behaviours all objects of that type will have.
An object is a specific, live instance of a class, created in memory using the 'new' keyword.
Instance variables are pieces of data that belong to a single object, and each object has its own independent copy.
Methods are blocks of code that define what an object can do, and you call them using the object reference, a dot, and the method name.
The dot operator (.) is used to access an object's instance variables and methods from outside the class.
You must use the 'new' keyword to instantiate an object; declaring a reference variable alone does not create an object.
Class names in Java conventionally start with a capital letter, while variable and method names start with a lowercase letter.
Default values for uninitialised instance variables are: 0 for int, 0.0 for double, false for boolean, and null for object references.
These come up on the exam all the time. Here's how to tell them apart.
Class
A blueprint or template that exists only in source code, not in memory at runtime.
Defined once and used to create many objects.
Does not hold any actual data values for instance variables.
Object
A concrete entity created from the class, allocated in memory at runtime.
Every object is an independent instance with its own set of data.
Holds actual values for each instance variable.
Instance Variable
Declared inside a class but outside any method.
Belongs to a specific object and has a default value (0, null, etc.).
Exists as long as the object exists.
Local Variable
Declared inside a method or constructor.
Belongs to the method and must be explicitly initialised before use.
Exists only while the method is executing.
Method with Return Type (e.g., int)
Must include a 'return' statement that sends a value back to the caller.
Can be used in expressions (e.g., int x = obj.calculate()).
The return type (e.g., int, String) specifies the kind of value returned.
Void Method
Does not return any value; no 'return' statement needed (or use 'return;' alone).
Called for side effects (e.g., printing or changing object state).
Declared with the keyword 'void'.
The 'new' Keyword
Allocates memory for a new object on the heap.
Calls a constructor to initialise the object.
Returns the memory address of the newly created object.
Declaring a Reference Variable
Creates a variable that can hold a reference to an object.
Does not allocate memory for an object.
The variable is initially null until assigned an object.
Mistake
A class and an object are the same thing, just different names.
Correct
A class is a blueprint that defines the structure and behaviour. An object is a concrete instance built from that blueprint that exists in memory with its own data.
This mistake happens because beginners hear 'class' and 'object' used almost interchangeably in casual conversation, and they do not immediately realise that a class is just the definition while an object is the actual running copy.
Mistake
I can change a class’s instance variable after creating an object, and the object will automatically update.
Correct
You cannot change a class definition while the programme is running. You can change the instance variable on a specific object, but that affects only that one object, not the class or any other objects.
People compare classes to templates in word processors: if you edit a template, all documents using it update. But in Java, once you compile the class, it is fixed. Objects are copies made from that fix, and changing the copy does not affect the original template.
Mistake
If I declare a variable like 'Person p;', I have created a Person object.
Correct
Declaring 'Person p;' only creates a reference variable that can point to a Person object, but no actual Person object exists. You must write 'p = new Person();' to create the object.
This confusion comes from the way we speak in everyday language: we say 'I have a dog' meaning a real dog exists, but in Java 'Dog d;' is more like having an empty leash with no dog attached.
Mistake
Instance variables can only be accessed from inside the class, never from outside.
Correct
Instance variables can be accessed from outside the class if they are declared with the 'public' keyword. If they are 'private', they can only be accessed from inside the class.
The exam highlights access modifiers heavily, but beginners often assume that because a variable 'belongs to' an object, it is hidden from the outside. They overlook the explicit 'public' or 'private' declaration.
Mistake
A class can only have one method.
Correct
A class can have as many methods as you need. There is no limit in the language. You can have hundreds of methods in one class.
Beginners who see small examples with one or two methods assume that is the rule. The exam uses simple classes, but you can add as many methods as the design requires.
Mistake
If I create two objects from the same class, they share the same instance variables.
Correct
Each object has its own separate copy of every instance variable. Changing the value in one object does not affect the other object at all.
People think of classes as shared documents in a team — one change affects everyone. But in Java, each object is its own isolated box of data, like separate filing cabinets.
Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.
A class is a blueprint that defines the structure (variables) and behaviour (methods) for a type of thing. An object is a concrete instance of that class created at runtime, which occupies memory and has its own values for the instance variables.
Yes. You must use the 'new' keyword to allocate memory and create an actual object. Simply declaring a reference variable (e.g., 'Student s;') does not create an object.
Your programme will throw a NullPointerException at runtime and crash. The object reference points to nothing (null), so there is no code to execute.
Only if the instance variable is declared with the 'public' access modifier. If it is 'private', you cannot access it directly from outside the class.
Numeric types (int, double) default to 0 and 0.0, boolean defaults to false, and object references (like String) default to null.
As many as your computer’s memory allows. There is no built-in limit. You can create hundreds, thousands, or millions of objects from a single class.
In most cases, yes. You use the dot operator with the object reference to access its methods and variables. There are some advanced cases (like reflection) but those are not in the 1Z0-811 exam.
You've finished Object-Oriented Programming: Classes and Objects. Continue through the 1Z0-811 study guide to build a complete picture of the exam.
Done with this chapter?