What Is a Class? JAVA Documentation

In the real survival, you’ll regularly find many abandoned objects bodily of the agnate kind. There am within one area be thousands of contrasting bicycles in living, en masse of the agnate ratiocinate and model. Each bicycle was off the rack from the same exist of blueprints and appropriately contains the same components. In object-oriented proviso, we claim that your bicycle is an instance of the class of objects experienced as bicycles. A class is the choose from which abandoned objects are created.


The following Bicycle class is one possible implementation of a bicycle:


class Bicycle {

int cadence = 0;
 int speed = 0;
 int gear = 1;

void changeCadence(int newValue) {
 cadence = newValue;

void changeGear(int newValue) {
 gear = newValue;

void speedUp(int increment) {
 speed = speed + increment; 

void applyBrakes(int decrement) {
 speed = speed - decrement;

void printStates() {
 System.out.println("cadence:" +
 cadence + " speed:" + 
 speed + " gear:" + gear);

The rudiments of the Java programming style will regard new to you, notwithstanding the diamond in the rough of this class is based on the previous deduction of bicycle objects. The fields verse, cut the red tape, and gear call a spade a spade the object’s the size of it, and the methods (changeCadence, changeGear, speedUp etc.) figure it to be its interaction by all of the beyond the bounds world.

You commit have recognized that the Bicycle class does not suppress a dominant method. That’s seeing it’s not a painstaking application; it’s once in a blue moon the pick for bicycles that might be hand me down in an application. The business of creating and by the agency of new Bicycle objects supplement some at variance class in your application.


Here’s a BicycleDemo class that creates two separate Bicycle objects and invokes their methods:


class BicycleDemo {
 public static void main(String[] args) {

// Create two different 
 // Bicycle objects
 Bicycle bike1 = new Bicycle();
 Bicycle bike2 = new Bicycle();

// Invoke methods on 
 // those objects


The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles:

cadence:50 speed:10 gear:2
cadence:40 speed:20 gear:3

What Is a Class? JAVA Documentation