The statements inside the methods block perform some particular task and then return an outcome to the caller. It is not compulsory for the method to always return something. The purpose of member functions is the reusability of the code. Your email address will not be published. Save my name, email, and website in this browser for the next time I comment. If the programmer does not define the constructor, the program automatically calls the default constructor.
It will initialize all the member variables to zero. Figure 1: Program with Constructor. In the above Employee class, there is a constructor called Employee. There are two instance variables as id and name. In line 8, there is a constructor, which has the same name as the class name. It initializes the id and name. In the main program, an employee object is created. Finally, the id and name values appears on the console.
A method is a set of statements to perform a certain operation. Using methods in the program makes it more manageable. It is possible to call the relevant method when required. Also, each method has a name to identify it. And, a method can accept a parameter or not.
After performing the task, the method can return a value. Here, if the method returns an integer, the return type is int. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Difference Between. Most visited in Java.
0コメント