Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

diff betwn method and constructer  RSS feed

 
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
}
 
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.
 
Ranch Hand
Posts: 1159
9
  • 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
 
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 ]
 
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.
 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!