Object Oriented Programming

Class Declaration in Java

Classes are a blueprint for creating individual objects that contain the general characteristics of a defined object type. A modifier may or may not be used to declare a class.

modifier class myClass { //class header

//field, constructor

//method declarations



The class body contains constructors for initializing new objects, declarations for the fields that provide the state of the class and its objects, and methods to implement the behaviour of the class and its objects.

public class Bike { 
    public int speed; 
    public int gear; 
    public Bike(int startSpeed, int startGear) { 
        this.gear = startGear; 
        this.speed = startSpeed; 

    public void setGear (int newValue) { 
        this.gear = newValue; 
    public void applyBrake(int decrement) { 
        this.speed -= decrement; 
    public void speedUp(int increment) { 
        this.speed += increment; 

See Also

Declaring Classes - Java Tutorials

Add to Slack

< Do-While Loop   |   Try Catch Block >

© 2019 SyntaxDB. All Rights Reserved.