How to Java programming- Classes & Objects

An object-based Java application is a Java application whose design is based on declaring classes, creating objects from them, and designing interactions between these objects. The object is the physical as well as logical entity whereas class is the logical entity only. A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support. Before you start this guide, It recommended looking our previous guide.

 

1.Class.

A class is a template for manufacturing objects. You declare a class by specifying the class keyword followed by a non-reserved identifier that names it. A pair of matching open and close brace characters ({ and }) follow and delimit the class’s body. This syntax appears below:

     class identifier {
          // class body
     }

 

A class in java can contain data member, method, constructor, block, class, and interface.

 

2.Object.

Once you have declared a class, you can create objects from it. An object is nothing more than a class instance. An object has three characteristics:

State: represents data of an object.
Behavior: represents the behavior of an object such as deposit, withdraw etc.
Identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But, it is used internally by the JVM to identify each object uniquely.

Here, we created a Student class that has two data members id and name. We are creating the object of the Student class by new keyword and printing the value of the object.

Create Java class

Java Object & Class

 

Object
     package FirstPackage;

     public class Student1{
         int id; //instance variable
         String name; //instance variable

         public static void main(string[] args){
              //creating an object of student
              Student1 s1 = new Student1();
              System.out.println(s1.id);
              System.out.println(s1.name);
         }
     }

 

Output
     0
     null

 

Constructors

When discussing classes, one of the most important sub-topic would be constructors. Every class has a constructor. If we do not explicitly write a constructor for a class, the Java compiler builds a default constructor for that class.
Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.
Following is an example of a constructor.

Constructors
     package FirstPackage;

     public class Student1{
           //constructor without parameter
           public void student1(){}
           //This constructor has one parameter 
           public void student1(String name){}
     }

Java also supports Singleton Classes where you would be able to create only one instance of a class.

Note − We have two different types of constructors. We are going to discuss constructors in detail in the subsequent chapters.

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...