Write a Program Example of Constructor in java - Technology369kk

 Example of Constructor


Example 1: 


class Constructor1{
    Constructor1(){
        System.out.println("Constructor Methods Called");
    }
    public static void main(String args[]){
        System.out.println("This is Example of Constructor ");
    }
    Constructor1 object = new Constructor1();         //creating object
}



OUTPUT: 


PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\allpraque> java Constructor1
This is Example of Constructor



Example 2: 


is is Example of Constructor

// Q2.  We have created the a student details where student class that have two parameters,
// We can have  any numbers of parameters in the constructor

class Student4{
    int id;
    String name;
    Student4(int i, String n){
        id = i;
        name = n;
    }

    void display(){
        System.out.println("Your Roll No is :-"+id+" and Name is "+ name);
    }
    public static void main(String args[]){
        Student4 s1= new Student4(111,"Shailesh");
        Student4 s2= new Student4(369,"Anshu");
        s1.display();
        s2.display();
    }
}


OUTPUT:- 

PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\allpraque> java Student4
Your Roll No is :-111 and Name is Shailesh
Your Roll No is :-369 and Name is Anshu





Post a Comment

0 Comments