package om.java4U; /* * **********************Difference Between - TopicsExpress



          

package om.java4U; /* * **********************Difference Between Method And Constructor************************** * @Author:OM PRAKASH * @Company:java4U * @Email:om.prakash.mck@gmail * */ public class Test_Method { public static void main(String[] args) { Student St = new Student(); St.Student(); St.Display(); /*First comment*/ //1.St.Name="1.JAVA4U"; //1.St.Rno=1; /*Second Comment*/ //2.System.out.println(" Name==:"+St.Name+" Rno==:"+St.Rno); /* * First run this program as it is and than after release this comment(First comment and than second comment) and run. * You may get no any value (1.JAVA4U,1) in your console and than release second comment and than run you get value as :1.JAVA4U,1. * It means the main use of constructor is to initialise the instance (other than static variable) variable . * * */ } } class Student { String Name; int Rno; Student() { Name = "java4u"; Rno = 123; System.out.println("**********Constructor in Student Class*********"); /* * This is Constructor Since,The method name is same as class name with no return type. * Remember that -------------- * 1." If a method name is same as class name without any return type ( NULL, also) than it is called Constructor of the class" * 2. "If a method name is same as class name with any return type (NULL, also) than it is called Method of the class" */ } void Student() { System.out.println("**********Method of Student Class***********"); System.out.println("--------Method Name Same As Class Name-----------"); System.out.println("Name:" + Name + " Rno:" + Rno); /* * This is Method but the name of the method is same as class name * No,problem Since here the Student is followed by return type void * So,Its become a Method not a constructor * Another main important thing is if method name and constructor name of same class is identical (same) than * we have to recognize depends up on the return type .If return type is avalable than it is called method or else constructor . */ } void Display() { System.out.println("-----Method Name Different as Class Name-------"); } }
Posted on: Sun, 09 Jun 2013 20:32:06 +0000

Trending Topics



Recently Viewed Topics




© 2015