Java Classes, Objects And Constructor

 An Object is a real world entity which has some properties and behavior. Object in Java is an instance of a class.

  While class in Java is a blueprint or a logical entity using which objects are created. 

Syntax;
       class ClassName{
             //Instance variables or fields
            // constructor: to initialize fields
            //Methods or behavior of class
       }

       Example:
          class User{
                 public int userId; //Instance variable
                 public String userName;
                 public User(){ //constructor
                         this.userId=1;
                 }
                 public void sayHello(){ //method
                        System.out.print("Hello");
                 }
           }

Creating Object: 
          Syntax: ClassName variableName= new ClassName();

           Example:
                User user = new User();
                 //Accessing methods of class using reference variable
                user.sayHello();
                //Accessing instance variable of the class using reference variable
                System.out.println(user.userId);


Ways to initialize object:
           We can use the following ways to initialize object in Java
           1. Using reference variable
                       class User{
                              public int userId;
                              public String userName;
  
                              public int getUserId(){
                                   return this.userId;
                              }

                              public String getUserName(){
                                  return this.userName;
                            }
                        }

                       class Program{
                              public static void main(String args[]){
                                     User user = new User();
                                     //Using reference variable i.e. user in this case 
                                     user.userId=1;
                                     user.userName="Joey";

                                     System.out.println(user.getUserId()+":"+user.getUserName()); //Output: 1:Joey
                              }
                       }

             2. Using Constructor 

                       class User{
                              public int userId;
                              public String userName;

                              public User(int userId, String userName){
                                      this.userId= userId;
                                      this.userName= userName;
                              }
  
                              public int getUserId(){
                                   return this.userId;
                              }

                              public String getUserName(){
                                  return this.userName;
                            }
                        }

                       class Program{
                              public static void main(String args[]){
                                     //Pass the values in the constructor itself
                                     User user = new User(1, "Joey");                       System.out.println(user.getUserId()+":"+user.getUserName());  //Output: 1:Joey
                              }
                       }

               3. Using setters methods 
                       class User{
                              public int userId;
                              public String userName;
  
                              public int getUserId(){
                                   return this.userId;
                              }


                              public void setUserId(int userId){
                                   this.userId=userId;
                              }
    
                              public String getUserName(){
                                  return this.userName;
                            }

                              public void setUserName(String userName){
                                  this.userName=userName;
                            }
                        }

                       class Program{
                              public static void main(String args[]){
                                     User user = new User();
                                     user.setUserId(1);
                                     user.setUserName("Joey");
                                     System.out.println(user.getUserId()+":"+user.getUserName());  //Output: 1:Joey
                              }
                       }

Now you know about how to create class and object in Java. Let's discuss about constructors.

Constructor:
          A constructor in Java is a special type of method which is used to initialize the object. 
Whenever we create an object using new keyword, constructor is called.

         Example:
              class User{
                     public User(){
                            System.out.println("Constructor is called...");
                     }
              }

              Now if you'll create the object in main method:
                    User user = new User();

                    //Constructor is called will be shown

            There are two types of constructor 
                   1. Parameter less constructor
                   2. Parameterized constructor

1. Parameter less constructor:
         It is also known as default constructor or no-arg constructor. A constructor is said to be a parameter less constructor when it doesn't have any parameter.
        Example:
              class User{
                     public User(){
                        System.out.println("Constructor is called...");
                     }
              }

Note: Java JVM provides default constructor by default.

2. Parameterized Constructor:
         A constructor is said to be a Parameterized constructor when it contains some specific number of parameters.
         It is very often used to initialize the value of instance variables.

         Example:
                       class User{
                              public int userId;
                              public String userName;
                              //constructor
                              public User(int userId, String userName){
                                      this.userId= userId;
                                      this.userName= userName;
                              }
                       }

           To create the object we can call it as:
                    User user = new User(1, "Ben");


Constructor Overloading:
            We can define multiple constructor for the same class.
 To know more about overloading check out our blog on Overloading.

      Example:
                       class User{
                              public int userId;
                              public String userName;

                               public User(){
                                    System.out.print("Hello World");
                              }
                              public User(int userId, String userName){
                                      this.userId= userId;
                                      this.userName= userName;
                              }

      Now we can initialize this user class object as 
               User user = new User();
          Or User user = new User(1, "Tom");

Continue learning: Inheritance in Java

   

Comments

Popular Posts