Sunday, 15 January 2012
JAVA : What is the difference between a constructor and a method?
Do you like this Article?
A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
Subscribe to:
Post Comments
(
Atom
)
Popular Posts
-
public - public means everyone can access it.That means it's in global scope. As, main method is called by JVM [ Java Virtual Machine...
-
throw is used to throw an exception in a program, explicitly . Whereas, throws is included in the method's declaration part, wi...
-
Singleton in one of the most popular yet controversial design pattern, in the world of object oriented programming. It's one of t...
-
Web Container / Servlet Container / Servlet Engine : In J2EE Architecture , a web container (also known as servlet container or ser...
-
Program compiles. But at runtime throws an error “NoSuchMethodError”.
-
Vector : It's synchronized. It's slower than ArrayList. It's generally used in ...
-
doGet(): protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, java.io.IOException – is a met...
-
In Java Programming Language , we must declare a variable name and type, before using it. The data type of a variable defines the th...
1 Responses to “ JAVA : What is the difference between a constructor and a method? ”
8 September 2013 at 18:18
Constructor:
Constructor must have same name as Class name.
Constructor does not have return type not even void.
Constructor does not support inheritance.
Constructor does not support overriding concept.
No need to call constructor explicitly it is called automatically at the time of object creation.
Generally we need constructor to write initialization logic.not business logic.
if we didn't provide constructor then JVM automatically add default constructor.
-----------------------------------------------
Method:
Method can have any name.
Method can have any return type.
We write method for modular programming.
In Program when repetitive statement occurred better to go for method.
it can be used to write business logic.
Need explicit calling by using object or Class name.
Post a Comment