Constructor In Java  

Constructor is a block of code similar to object. Constructor is created when creating Instance of Class.

There are two type of Constructor::
1. Default Constructor
2. Parameterized Constructor



Program Example


class B {
    int age;
    String name;
B(int a, String b){ // create a constructor.........
    age=a;
    name=b;
}
void display(){
    System.out.println("The age of " + name + " is " + age);
}
    
}

public class MyClass {
    public static void main(String args[]) {
        B obj = new B(15,"Karan");
        B obj1 = new B(21,"Arjun");
        
        obj.display();
        obj1.display();
     
    }
}

Comments

Popular posts from this blog

About RAM

Class and object in c++