What is a class in Java?

What is a class in Java? A simple question to answer. Class is a basic unit of Java programming. A class in Java is a structure which contains programming constructs. Programming constructs are small pieces of code with which a program is built; like bricks, cement and sand are constructs to construct a building. The programming constructs are mainly constructors, methods and variables. So finally, to say, a class is made up of or embeds constructors, methods and variables, all doing a predefined task.

To access these constructs, we require a handle which points (refer) all these. This handle is known as object. So, it is necessary to create an object of the class to access these constructs. A class gives a boundary for the whole code. Java demands you to write any amount of code within its boundaries delimited by two braces like { and }.

Following gives a specimen of a class with a constructor, a method and some variables. An example for What is a class in Java?
class Employee
{                                     // starting brace of the class (delimiter)
  int id;                             // variables
  String name;                        // three are given here
  double salary;

  Employee()                          // a constructor
    System.out.println("\nFrom Constructor");                     // some code here that assigns values to the above 3 variables

  void calculate(int radius)          // a method
    System.out.println("Circle Area: " + Math.PI*radius*radius);  // some code here to calculate the total salary of an employee
  public static void main(String args[])
    Employee emp1 = new Employee();   // create an object; it calls constructor implicitly
    emp1.calculate(20);               // call the method with object

    emp1.id = 1234;                   // now assign values to instance variables
    emp1.name = "Srinivas";
    emp1.salary = 9876.54;
    System.out.println("Employee ID: " + emp1.id + " Employee Name: " + emp1.name + " Employee Salary Rs." + emp1.salary); 
}                                     // closing brace of the class (delimiter)

What is a class in Java?Output Screenshot on What is a class in Java?

Employee emp1 = new Employee();

This is the style of creating Java object; on both sides class name Employee will be there. Object is emp1 and it is a handle to access constructor, methods and variables in the class. Observe, all the code is written within the braces of the class declaration.

For more Examples and more Explanation refer the following links of this same Web site..

1. Java Example Compilation Execution
2. Java Local Instance Variables
3. Using Variables from Methods
4. Data Binding Data Hiding Encapsulation

After going through the above links, slowly go deep into this site step-by-step. Better have help from your friend knowing Java or a good Lecturer from a good Institute, so that learning will be faster and orderly.

Leave a Comment

Your email address will not be published.