Object and Classes in Java : Basics and Syntax

As mentioned in the topic today we are going to learn about classes and its object. It will include definition of classes how it is declared how and why they are utilized. In Java the OOP concept includes a part called classes a vital part of java as well making the program much more faster to compile. A very basic thing to be noted objects are logical as well as physical entity whereas classes are logical entity.

Objects

Since childhood we know that objects are those entities that has state and a particular behavior for example books, computer, etc; This was an example from our day to day life. As mentioned above its can be logical or physical, i.e; tangible and intangible. We can distinguish an object with three characteristics:
  • State: Represents data of the object.
  • Behavior: Represents functionality of the class
  • Identity: Every object is identified by an unique identity but the difference is that the value is hidden in case of computers.
Now relating both classes and objects. Classes are blueprints for which objects are created. Hence, objects are instances of objects defined.

Classes

A class is a collection of objects with similar properties. In a class we can define multiple data under one name. It is a template for which an object is defined. It is a logical quantity and can never be physical.

Syntax

class class_name{
   data_to_be stored;
   variables;
   functions;
}

Instance Variable

Variables defined inside the class but outside the method are instance variables. Memory allocation to these variable are not in the compiling time but in the run-time. This is the reason relating the English language it was named as instance variable. Because its declaration is at the run-time.

"new" keyword

This keyword is used to allocate data in the runtime.


Objects and Classes in Main function

There is a simple program following containing  a class named student with data members as ID and Name. This program is to simply display the data calls the class using an object in the main function. this class definition is for the public java class. So, here we go:
class Student{       //file name is student
            int id;
            string name;

public static void main(String[] args){
       Student s1= new Student();  //this creates object for the class
       System.out.println(s1.id);  //displays ID part of the class
       System.out.println(s1.name);  //displays Name part of the class 
    } 
}
Now we shall do the same program but without using the public class that is a different class. This class can be defined within the main class as well as outside the main class. So, here we go:
class Stud{
        int id;
        string name;
}
public class Student{
 public static void main(String[] args){
   Stud s1=new Stud();
   System.out.println(s1.id);
   System.out.println(s1.name);
   }
}
we can declare the objects of class in two ways one is directly by programmer while defining the variables and the other way is by user through scanner class. Assuming all definitions are correctly done here comes a short definition  of class:
class Student{
  int a;
  void accept{
   scanner  sc=new Scanner(System.in);
   a=sc.nextInt();
  }
}
In the main function we call the data of class through an object. So to store more than one set of values we can define more number of classes in the main function. Like for the above class the main program can be as follows:
public static void main(String[] args)
{
    scanner sc=new scanner(System.in);
    Student s1=new Student();  //Object 1
    Student s2=new Student();  //Object 2  
    s1.a=nextInt();   //statement 1
    s2.a=nextInt();   //statement 2
}
Here statement 1 as well as 2 both are valid. Since s1 and s2 are two different objects so two different set of values are stored in it. There are loads of things in classes shall be continued in our other posts.

Share:

0 comments: