This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes diff betwn method and constructer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "diff betwn method and constructer" Watch "diff betwn method and constructer" New topic
Author

diff betwn method and constructer

ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
hi friends do any one knows what is diff betwn method which has same name as
class name and return type void and construter

eg
public class C1
{
public C1(){ <<<<-----constructer
//some initialization code here
}

public void C1()<<<----method name same as class name and return nothing
{
//some code
}
Andrew Nomos
Greenhorn

Joined: May 31, 2005
Posts: 18
A constructor is called during object instantiation and cannot be invoked explicitly.
A method represents object behaviour and can be invoked at any time.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
Originally posted by Andrew Nomos:
A constructor is called during object instantiation and cannot be invoked explicitly.
A method represents object behaviour and can be invoked at any time.


Well...
If you have a constructor declared...
public MyClass(String s, int i)
you can "explicitly" invoke it with...
MyClass myObj = new MyClass("Andrew", 42);

Granted, you're actually expilicitly invoking the "new", but there is an obvious correspondence between the constructor signature and the code written to access it.

Other difference:
- Constructor chaining. A call to super() or super(some args) must be the first statement in a constructor if one appears at all. If the first executable statement isn't a call to one of the superclass's constructors, then a call to super() is automatically added.
- Explicit "return type". Calling "new Component()" will definitely give you a Component. However if you have a method with a declared return type of Component, it is free to return a Component or an instance of any of its subclasses.

There's probably more.

Ryan
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
www.google.com , really it doesn't bite.

[ June 07, 2005: Message edited by: Hentay Duke ]
[ June 07, 2005: Message edited by: Hentay Duke ]
Pete Low
Greenhorn

Joined: May 25, 2005
Posts: 10
Hi,

Wrt the code :

public void C1()<<<----method name same as class name and return nothing
{
//some code
}


I thought the keyword void should not be included as it is a constructor.
Only different parameters are allowed.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Java does not exclude the use of the class name as a method name. Having the void return type defines that as a method and not a constructor. This is completely worthless, you should never do it, but you may see it on something like the SCJP.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: diff betwn method and constructer
 
Similar Threads
what is diff betwn Object and instance
Deep Copies?
random number gene betwn 2 no's
Help with constructor
Hibernate annotations one to many relationship with composite primary key