What is class, object, variable, field, method and constructor in Java?

Java is an object-oriented programming language. There are some terms and concepts that you must know because these concepts would be applied in next chapters in different ways. Below is the introduction of these terms and concepts with precise code examples.

What is class in Java?

A building block of an object-oriented programming language like Java is class. A class is a template or blueprint from which individual objects are created.

Watch Java Video Tutorials for Beginners 

A class describes the behavior of objects and data associated with these objects or instances of that class. A class can have different modifiers like public, protected, private, abstract, final etc. We will discuss access modifiers later in this chapter.

Let’s take an example of a Car which has four wheels, one steering, one windscreen etc. These are components or parts of a car. Whenever we need to make a car, we will assemble all these parts. If we try to map this example in Java, we will consider Car as class that can have various properties. Car class will provide us a skeleton of a car. Car class will tell us that we have to make a sedan car, with four wheels, four doors, 1 windscreen, 1 steering etc.

1 public class Car{   //initialization of class                    
2 }                 //end of class

What is object in Java?

An instance of a class is called object of that class. An object can hold state and behavior.

If we consider again the example of Car class. A car can have multiple states like its maker, model, color etc. and it can have multiple behaviors like it can move forward, backward, turn left or turn right, convertible etc. When we go to a showroom to buy a Car, we specify the state and behavior we require and buy the car. A specific maker’s car with specified state and behavior is object of class Car.

Watch Java Video Tutorials for Beginners 

1 public class Car{        //initialization of class
2   
3    public Car object1;     // object declaration
4                     
5 }                 //end of class

What is a variable or field in Java?

A piece of memory, that stores information, data or value, required by your Java program to execute to complete its job is called a variable. Variable is also called field in Java. The states of an object are stored in variables. Watch Java Video Tutorials for Beginners 

For example, in class Car, we can have fields like maker, model, color, type etc. that hold information about particular maker’s car of particular model with specific properties.

1 public class Car{ //initialization of class
2   
3    public int number1; // variable declaration 
4                     
5 }                 //end of class

What is method in Java?

To separate functionalities related to one class, we use methods. A method contains code which is executed when it is called or invoked anywhere in a class.

A method is a sub program, it is small component or unit of a class. A method can also have parameters and it can return some values. Some methods are void methods and do not return anything. A method has a definition. This definition is used to invoke that method.Watch Java Video Tutorials for Beginners 

For example, in class Car, we may have methods to define functionalities like moveForward, moveBackward, moveLeft, moveRight and convertCarType, each of which contains code to perform a particular task.

1  public class Car{   //initialization of class
2    
3     public Car object1;   // object declaration
4   
5   
6   public void moveForward(){    //void method
7       //method body
8   }
9   public void moveBackward(int val1, int val2){//method with parameters
10      //method body
11  }
12  public int moveLeft(){ //method (returns int)
13      //method body
14      int field1;
15      
16      return field1;
17      
18  }

//method gets parameter & returns int value

19  public int moveRight(int val){  
20      //method body
21      
22      return val;
23  }
24  
25                     
26 }                 //end of class
27

What is a constructor in Java?

A constructor is similar to method. It is a sub-routine. It is invoked when an object or instance of a class is created. It initializes the object of class. Every class has a constructor by-default.

You can also explicitly write constructor in a class. A class can have multiple constructors. A constructor of a class has same name as the name of class in which it has been declared. None of the two constructors can have same definition, it means same number of parameters with same datatype of parameters cannot be used in two constructors. Either the number of parameters should be different or datatype of parameters should be different. The difference between method and constructor is, a constructor does not return any value while a method may or may not return a value.

Country.java

1   public class Country{
2  
3   //instance variables
4   public String country;
5   public String lang;
6   public int largest_currency_note;
7   
8   //this is default constructor
9   //we can write it explicitly
10  //otherwise compiler will
11  //automatically add it.
12  
13  public Country(){
14      
15      System.out.println("Default constructor is called");
16  }
17  
18  //this constructor takes country name
19  //to create an object of Country class
20  public Country(String country){
21      
22      //initializing variables
23      this.country = country;
24      System.out.println("You select country: "+this.country);
25
26  }
27  
28  //this constructor takes country & language
29  //to create an object of Country class
30  public Country(String country,String lang){
31      
32      //initializing variables
33      this.country = country;
34      this.lang = lang; 
35      System.out.println(country+" has language = "+this.lang);
36
37  }
38  
39  //this constructor takes country & currency
40  //to create any object of Country class
41
41
42  public Country(String country, int largest_currency_note){
43      
44      //initializing variables
45      this.country = country;
46      this.largest_currency_note = largest_currency_note; 
47      System.out.println(this.country+"'s largest currency
                    note is of "+this.largest_currency_note);
48
49  }
50 
51 
52}

Main.java

1   //initialization of class
2   public class Main{        
3 
4       
5       // main method
6       public static void main(String[] args){
7           
8           new Country();
9           new Country("Pakistan");
10          new Country("Pakistan","Urdu");
11          new Country("Pakistan",5000);
12      
13      }       //end of main method
14   }                 //end of class

In class Country, at line 13, we have default constructor. At line 30 and 42, we have same number of parameters but different datatype of parameters.

 

One Reply to “What is class, object, variable, field, method and constructor in Java?”

Leave a Reply

Your email address will not be published. Required fields are marked *