Ch 10 · Objects & Classes · Java

Topic 10 of 13 in Java — Complete Tutorial (Beginner) — 3 lessons.

Classes and Objects

Java is built around objects. A class is a blueprint that bundles data (called fields) with the methods that work on that data. From one class you create many objects (instances), each with its own data. The example defines an Animal class with fields, a constructor (the special method that sets up a new object), and methods. You create an object with the new keyword.

Fields, Constructors & this

A field is a variable that belongs to an object — it stores that object's data and lasts as long as the object does. The constructor has the same name as the class and runs automatically when you create an object with new, setting up the initial field values. The keyword this refers to "the current object," which lets you tell apart a field from a parameter that share the same name, as in this.name = name.

Many Objects from One Class

The power of a class is that you can create as many objects from it as you like, each holding its own separate data while sharing the same methods. Storing many objects in an array or ArrayList and looping over them is one of the most common patterns in all of programming — a class list, a product catalogue, a list of accounts.

All topics in Java Beginner

  1. Introduction to Programming & Java
  2. Your First Java Program
  3. Variables — Your Data Containers
  4. Data Types — Kinds of Values
  5. Operators & Expressions
  6. Conditional Statements
  7. Loops & Iterations
  8. Methods
  9. Arrays & Lists
  10. Objects & Classes
  11. Strings — Working with Text
  12. Exception Handling
  13. Mini-Projects & Practice