• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Method declaration

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it necessary for a method to declare return type in its declaration? If not then what would be the difference between the method and the constructor of a class, the method name being the same as the class name.
Thanks.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi geeta,
well for your query it is very much required in Java to declare the return type of the method in its declaration. Therefore it is mandatory, otherwise compiler would flag an error.
This is different from C where the default return type is int.
But in java there is nothing like this.
Hope this will solve your query
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by geeta rai:
Is it necessary for a method to declare return type in its declaration? If not then what would be the difference between the method and the constructor of a class, the method name being the same as the class name.
Thanks.


Oh well, I believe that in Java there is no such thing as method declaration... There is only method itself and we can call from anywhere in the class...
For return type, u can use "void" to return nothing and you may want to return from the method by using

Hope this helps..
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Geeta,
If the method name in question differs from the classname simply
by the case of its letters it won't be a constructor, you will
therefore get a "invalid method declaration, return type required"
compile error when no return type is specified.
The constructor (which is named identically like the class)
is the only method with no return type.
Gian Franco
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gian Franco Casula:
The constructor (which is named identically like the class)
is the only method with no return type.


I think the constructor should not be called method...
Method should be called as method and constructor should be called as constructor... Constructor is not the method with no return type, buddy...
Should methods with no return type be with "void" keyword?
Just try to clarify some important point....
 
Gian Franco
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I respect your opinion
but in the litterature I'm currently
studying at one point the constructor
is called a (special) method
(B. Eckel's Thinking in Java).
thanks for it pointing out,
Gian Franco
reply
    Bookmark Topic Watch Topic
  • New Topic