Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

diff betwn method and constructer

 
ganesh pol
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1064
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic