• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

constructors in java

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,
i have a doubt on java constructor.
the question is i have a class named example,
for this class which one will be the constructor
example() or void example().pls help me to clear this doubt.if both are constructors means is there any difference between them?


thx
jai
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one's a contructor
the other (with a return type) is a method.

Next time, please take care to post your question in the correct forum(This should belong to Java Beginners forum rather than the Servlet forum). You would get faster responses that way. Also, it would save the moderators the trouble of shifting it to the appropriate forum.

Thanks,
Ram.
 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Jai, Constructors should not have return type even void.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Java in General (beginner)
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
constructor cannot return anything. so you can safely define
example(){}

however you can overload a constructor
example(String name){}
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing to note is the name of the constructor is the same as the class name i.e. class Example (with a capital letter for a class name) has a constructor
Example( )
{
}//end constructor Example

which never has a return type i.e. void, String, int

If it has a return type i.e.

public void example ( )
{
}//end method example which returns nothing

or

public String example ( )
{
String name = "Fred";
return String
}//end method example which returns a String


.....then it is a method.
 
Have you no shame? Have you no decency? Have you no tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic