Loading, please wait...

A to Z Full Forms and Acronyms

Class-based programs of Java-part 1

May 19, 2021 Java, Classes, Constructors, shibam dhar, 6532 Views
In Java every program has at least one class. All the programs are written in Java acts as a model of the real world having at least one or more than one class. To process it we can create several instances of a single class or multiple classes.

Class-based programs of Java-part 1

In Java every program has at least one class. All the programs are written in Java acts as a model of the real world having at least one or more than one class. To process it we can create several instances of a single class or multiple classes.

For example, in an office there can be workers at different levels like cleaners, desk-workers and more, they all can be referred to as classes. Among these categories there can be many people belonging to one of the mentioned categories, all these people are referred to as instances of their respective class.

Making a single class and creating several instances of that one makes things easy and creates a neat flow of understanding the working of Java program and since Java is an OOP (object-oriented program) everything revolves around the object and their interactions. These objects have states and behavior. Classes are a blueprint for objects. Blueprints detail the general structure.

General Syntax

So, what exactly the classes are consist of? The classes of Java have sets of instructions. The instructions are given by the user while creating a class. These instructions decide that when an instance of this class will be created how it is supposed to behave, and what information is to be stored.

There are some pre-defined classes of Java. One of them is systemSystem is used to print our log to the screen.

The syntax of class:

public class Office {

 // scope of office class starts after the curly brace

 

   public static void main(String[] args) {

     // scope of main() starts after curly brace

 

     // program tasks

 

   }

   // scope of main() ends after curly brace

 

 }

 // scope of office class ends after the curly brace

In the above syntax, there is the class of OFFICE which has an access level of PUBLIC which allows other classes to interact with this class. This class has a main() method, which lists the tasks performed by the program. main() runs when we execute the respective compiled class file.

Constructors in a Class

Now that the class is created, the next is to create constructors. The work of the constructor is to provide rules for instances that are to be created in the class. The instance of the object of the class is created with the help of a constructor and this constructor is created inside the class only.

The constructor has a very basic syntax, it shares the same name as the main class name. To create an instance, we need to call or invoke the constructor within main(). 

For example:

public class Office {

 

   public Office () {

     // instructions for creating a Office instance

   }

 

   public static void main(String[] args) {

     // Invoke the constructor

     Office employees = new Office (); 

   }

 }

In the above example, we can break it into simple form by saying that a variable names employees is created which doesn’t have a data type like int or float but rather has a reference data type. By reference data type it means the value of our variable is a reference to an instance’s memory address. During its declaration, the class name is used as the variable’s type. In this case, the type is Office. After the assignment operator, (=), we invoke the constructor method: Office(). The keyword new is used to indicate that we’re creating an instance. Omitting new causes an error.

A to Z Full Forms and Acronyms

Related Article