Page 255 - IT-802_class_12
P. 255

}

                //getter method to retrieve the roll number
                public int getRollno() {
                 return rollno;
               }

              }
            public class main {
                public static void main(String[] args) {
                  Student s1= new Student(); //object of the class is created

                 s1.setName(“Ananya”);
                 s1.setRollno(12);
                  System.out.println(s1.getName());//printing the value returned by getName()
                   System.out.println(s1.getRollno());//printing  the value returned  by
                    getRollno()
                 }

               }































              3.13 conStructorS
            A data member that is declared but not initialized before using, is assigned a default value by the compiler usually either
            zero or null. However, it is good programming practice to always initialize variables before using them. A special method
            member called the constructor method is used to initialize the data members of the class (or any other initialization is
            to be done at time of object creation). The constructor has the same name as the class, has no return type, and may
            or may not have a parameter list. Whenever a new object of a class is created, the constructor of the class is invoked
            automatically. We do not call the constructor explicitly.







                                                                               Fundamentals of Java Programming  253
   250   251   252   253   254   255   256   257   258   259   260