Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Constructor in Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 211
    Comment on it

    Java Class Constructor

    1. It is special kind of function, but doesn't have a return type.

    2. It has the same name as class name.

    3. Constructor is used to initialized the variable of class.

    4. we can't call the constructor with the help of object. If we do so, then it will give compilation error.

    5. Constructor is automatically call when we create the instance/object of class.

    For Example:

     Class abc
            {
                int a;
                abc(int b)     //point 1 & 2
                {
                    b=a;  //point 3
                }
                void display()
                {
                    System.out.println(a); //output= 9
                }
            }
    
            class call
    
            {
                public static void main(String a[])
                {
                    abc obj= new abc(9);  //point 5
                    obj.display();
                    obj.abc(9); // point 4 , compilation error
    
                }
    
            }
    

    Note: If we don't write the constructor explicitly, then default empty (No-Argument) constructor will be generated by Compiler.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: