« Previous Page Next Page »
    

Introduction to Java Object and Class.

POSTED

AUTHOR: VARUN SINGH

In this chapter, we will learn about Objects and Classes which are basic concepts of Object-Oriented Language which revolve around the real life entities.

Objects in Java:

An entity that has state and behavior is known as an object. Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class.


Classes in Java:

A class is a blue print of an object. It is used to make objects. It tells the JVM how to make an object of that particular type. Each object made from that class can have its own values for the instance variables of that class.


Whats is the difference between an Object and Class.

A class is not object, but it is used to make an object of that particular type. Each Object created from class can have its own value for the instance variables of that class.


java object and classs


Object and Class: Initialization through reference:

Let's see a simple example where we are going to initialize object through reference variable.

File: TestStudent.java

class Student{  
 String name;
 int age;
 String color;
 String sex; 
}  
class TestStudent{  
 public static void main(String args[]){  
 
  Student s1=new Student();  
  Student s2=new Student();  
   
  s1.name="John"; 
  s1.age=12;
  s1.color="Fair";
  s1.sex="Male";
  
  s2.name="Sophia"; 
  s2.age=10;
  s2.color="Fair";
  s2.sex="Female";
  
  System.out.println("Name:"+s1.name);
  System.out.println("Age:"+s1.age);
  System.out.println("Color:"+s1.color);
  System.out.println("Running:"+s1.sex);
   System.out.println("\n"); //printing new line  
  System.out.println("Name:"+s2.name);
  System.out.println("Age:"+s2.age);
  System.out.println("Color:"+s2.color);
  System.out.println("Running:"+s2.sex);
 }  
}

Output:

Name:John
Age:12
Color:Fair
Running:Male

Name:Sophia
Age:10
Color:Fair
Running:Female




Object and Class: Initialization through method:

In this example, we are creating the two objects of Student class and initializing the value to these objects by invoking the insertRecord method

File: TestStudent1.java

class Student{    
 String name;
 int age;
 void insertRecord(String n, int a){  
  name=n;  
  age=a;  
 }  
 void displayRecord(){
	 System.out.println("Name: "+name+" "+"Age: "+age);
	 }  
}  
  
class TestStudent1{  
 public static void main(String args[]){  
 
  Student s1=new Student();  
  Student s2=new Student(); 
  
  s1.insertRecord("John",12);  
  s2.insertRecord("Sophiya",10);  
  
  s1.displayRecord();  
  s2.displayRecord(); 
   
 }  
}

Output

Name: John Age:12
Name: Sophia, Age:10;




The End:

Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section. We will be very happy to hear that.

If you like my tutorial, please follow us on facebook, Google + and Twitter. I need your support to continue.

« Previous Page Next Page »

Comments: