Class and Object in Java are coupled together for Object Oriented approach.

Difference between Class and object

Class is not an object but it is used to construct objects. A class is an blueprint for an object. A class tells JVM on how to create object of particular class type. Multiple objects can be created from a single class.

For example, if you take a look at the “creating contact” page of your mobile device or diary – the template used to store information such as first name, last name, phone number, address, city, state and etc., is considered as class (a template) and each contact in your contact list is called object. You might have contacts of your friends, family members, business peoples – each contact in your list is considered an object created from the defined template class.

How to create object?

You need to have two classes. One class (real class) for type of object to create  and another class (tester class) to test your new class. The tester class is where you need to put main() method and in the main() method you create and access objects of your new class type.

Example 1:
Class for Address Book (real class)
public class AddressBook {

  String firstName;
  String lastName;
  String phoneNumber;
  
  public void showContactAsString() {
    System.out.println("Name is "+firstName+" "+lastName+" with phone number "+phoneNumber);
  }
}
Class to test Address Book (tester class)
public class TestAddressBook {

  public static void main(String[] args) {
    AddressBook addressBook = new AddressBook();
    addressBook.firstName = "Tim";
    addressBook.lastName = "Benz";
    addressBook.phoneNumber = "91-33333-22222";
    
    addressBook.showContactAsString();
  }
}
Output
Name is Tim Benz with phone number 91-33333-22222

In main() method of above example you could see, new object of AddressBook class is created and using the dot (.) operator, the real class variables and methods are accessed. The dot(.) operator gives you access to an object’s state and behavior (instance variables and methods).

A class describes,

what an object knows i.e., variables

and what an object does i.e., methods

Does all objects give us same results for method call?

Every object of an particular class has the same methods, but the methods can behave differently based on the values of the variables. Depends on the object’s variable value, method provide different results. The method code however is same. Example 2 gives us different result because of variable value while compared to Example 1, yet the code is same.

Example 2

public class TestAddressBook {

  public static void main(String[] args) {
    AddressBook addressBook = new AddressBook();
    addressBook.firstName = "Karl";
    addressBook.lastName = "Marx";
    addressBook.phoneNumber = "91-44444-55555";
    
    addressBook.showContactAsString();
  }
}

Output

Name is Karl Marx with phone number 91-44444-55555

The two uses of main() method

  1. to test your real class
  2. to lauch/start your Java application

let’s review the meaning of each keyword of the common signature:

public static void main(String[] args) { }
  • public – access modifier, meaning global visibility
  • static – the method can be accessed directly from the real class, we don’t have to instantiate or create an object to have a reference and use it
  • void – means that this method doesn’t return a value
  • main – the name of the method, that’s the identifier JVM looks for when executing a Java program

As for the args parameter, it represents the values received by the method. This is how we pass arguments to the program when we first start it. The parameter args is an array of Strings

Therefore, we explained what is class, objects, how to create object, do method call using dot operator and what is main() method.

Please follow and like us:
error