Crazy Online Tutorial.


Following are the some Advantages of blogsites

  • Easy to find more updates.
  • Notify you via email when post any tutorial.
  • When we do contest then get notification easyly
by

Java OOPs Concept - Consstructors

Constructor
Java allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor. A constructor is a spacial type of method which is initializes an object immediately upon creation.

Simple rules for creating constructor: 
constructor looks like strange because of 
1.  There are no return type class . It happens because the implicit type of a class , contractor is the class type itself.
2. Constructor name must be the same name as its own class name.


Constructor Types:

There are two types of Constructor

1. Default constructor
2. Parameterized constructor

Default Constructor
A constructor that have no parameter is known as default constructor. it is auto create class if you don't create this type of class.


                   package com.tariqul.StudentDetails;

                   public class StudentRegistration{

                            private String Name;
                            Private String Address;
                               . . . . .  . .. . . .. . .. . . . . . ..
   
                              StudentRegistration(){
 
                               }
                   }

if you don't create any constructor then compiler create default constructor automatically. Depending on your attributes type, defaults constructor provides null or 0 or any other defaults value.
 

                  package com.tariqul.StudentDetails;

                   public class StudentRegistration{

                            private String Name;
                            Private String Address;
                               . . . . .  . .. . . .. . .. . . . . . ..
   
                   }

when you do this, It provides you some default value. as like

  package com.tariqul.StudentDetails;

                   public class StudentRegistration{

                            private String Name;
                            Private String Address;
                               . . . . .  . .. . . .. . .. . . . . . ..
   
                              StudentRegistration(){
                                Name = " ";
                                Address = " ";

                               }
                   }
 
Parameterized constructor

 We can pass values by using  parameter . 


                 package com.tariqul.StudentDetails;

                   public class StudentRegistration{

                            private String Name;
                            Private String Address;
                               . . . . .  . .. . . .. . .. . . . . . ..
   
                              StudentRegistration(String name, String address ){


                               Name = name;
                               Address = address;
 
                               }
                   }
you can get the value by calling this constructor.
                  StudentRegistration st_registration= new StudentRegistration("Tariqul", "A");

when the object is created , it handed over "Tariqul" and "A" to the constructor .

Difference between constructor and method


0 comments:

Post a Comment