In object-oriented programming, a class is a blueprint for creating objects that encapsulate data and behavior. It defines a set of attributes (data members) and methods (functions) that the objects created from it will possess. A class provides a template or structure for objects, allowing us to create multiple instances of the same type of object, each with its own state and behavior. In essence, a class defines the properties and capabilities of a particular type of object, and objects created from the class are known as instances of that class.
In simple words, a class is a blueprint from which individual objects are created/constructed.
Things an object knows about itself are called instance variables.
Things an object can do are called methods.
A class is not an object (but it’s used to construct them).
A class is an object’s blueprint. It instructs the virtual machine on how to create an object of that type. Each object derived from that class can have its own values for the class’s instance variables. For example, you could use the Button class to create dozens of different buttons, each with its own color, size, shape, label, and so on.
What do Java Classes Contain?
A class in Java can contain:
- Data Member(instance variables or fields)
- Method
- Constructor
- Block
- Class
- Interface
How to Declare a Class in Java?
A class is declared by using the class keyword followed by a non-reserved identifier that names it. The class’s body is delimited by a pair of matching open and close brace characters ( and ). Consider the standard syntax for declaring a Java class:
class <class_name>{
data member;
method;
}
The first letter of a class’s name is always capitalized, by convention, and all subsequent characters are lowercase (for example, Button). If a name is made up of several words, the first letter of each word is capitalized (such as ButtonDemo). This naming convention is known as camel casing.
When “designing” a class, consider the objects that will be constructed from that class type. Think about:
- things the object knows(data that it contains)
- things the object does(how to manipulate that data or do some actions).
In this lesson, we want to design a class called Button. This Button class is the blueprint, using which we can create buttons. We have already shown you the UML Diagram of the Button class. We are now going to implement this class in Java.
class Button{
String label;//field or instance variable#1
String color;//field or instance variable#2
//method#1
public void setColor(String color){
this.color = color;
}
//method#2
public void seLabel(String label){
this.label = label;
}
//method#3
public void doPress(){
//instructions go here
}
//method#4
public void undoPress(){
//instructions go here
}
}
The Java programming language syntax will be unfamiliar to you, but the design of this class is based on the previous discussion of button objects. The object’s state is represented by the fields(label and color), and its interaction with the outside world is defined by the methods (setColor, setLabel, doPress, and undoPress).
You might have noticed that the Button class lacks a main method. That's because it's not a complete application; it's simply a blueprint for buttons that could be used in an application. It is the responsibility of another class in your application to create and use new Button objects.
Write a Class
class Button{
String label;//field or instance variable#1
String color;//field or instance variable#2
//method#1
public void setColor(String color){
this.color = color;
}
//method#2
public void seLabel(String label){
this.label = label;
}
//method#3
public void doPress(){
//instructions go here
}
//method#4
public void undoPress(){
//instructions go here
}
}
Write a Tester Class
class ButtonTest{
public static void main(String[] args) {
}
}
Make an Object
class ButtonTest{
public static void main(String[] args) {
Button buttonOne = new Button();//creating first button
buttonOne.setColor("red");//setting color field
buttonOne.seLabel("RED Button");//setting label field
System.out.println("The label of buttonOne is : "+buttonOne.label+" and it's color is: "+ buttonOne.color);
}
}
We have written the complete class in a single source file called ButtonTest.java for simplicity.
class Button{
String label;//field or instance variable#1
String color;//field or instance variable#2
//method#1
public void setColor(String color){
this.color = color;
}
//method#2
public void seLabel(String label){
this.label = label;
}
//method#3
public void doPress(){
//instructions go here
}
//method#4
public void undoPress(){
//instructions go here
}
}
class ButtonTest{
public static void main(String[] args) {
Button buttonOne = new Button();//creating first button
buttonOne.setColor("red");//setting color field
buttonOne.seLabel("RED Button");//setting label field
Button buttonTwo = new Button();//creating second button
buttonTwo.setColor("green");//setting color field
buttonTwo.seLabel("GREEN Button");//setting label field
System.out.println("The label of buttonOne is : "+buttonOne.label+" and it's color is: "+ buttonOne.color);
System.out.println("The label of buttonTwo is : "+buttonTwo.label+" and it's color is: "+ buttonTwo.color);
}
}
To compile: javac ButtonDemo.java
To run the program: java ButtonTest
Output:
The label of buttonOne is : RED Button, and it's color is: red
The label of buttonTwo is : GREEN Button, and it's color is: green
If you are already OO savvy, you’ll know we’re not using encapsulation. We will go there later.
Look carefully at the above program. Always remember, to compile a source file, you must give the name of the source file. In other words, the name you gave while you saved the file in the directory. On the other hand, to run the program you have to give the class name where the main() method resides. Otherwise, your JVM can not find the main() method and hence can not start the program.