• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with method  RSS feed

 
Chris Davies
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i've the following class yet when i try to compile it -


it gives me the following error



what the problem with the method declaration ?

cheers

chris
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The keyword 'return' should never be in a method declaration. Did you mean

public void Uselist()?
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or did you want to define a constructor, in which case it would just be:

You can then call your code by including the following in main():

Jules
 
Chris Davies
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks


no, i meant to put public Uselist() {..}. I tried using public void Uselist() but when tried to call use the method within a jsp page (the class was in WEB-INF/classes) and the class was inititialised using the following



i get the following error message




whats the problem with just using public Uselist(){...} and why am i getting this error ?

cheers

chris
 
Liam Tiarnach
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It tell you the problem here...

Can't convert void to java.lang.Object.
out.print( listvalue.Uselist() );


The print() method requires some type a data to be passed to it, either a primative such as int, double and so on, or an Object... when passes an Object, the print() method calls that Objects toString() method...

Okay, well enough of that... That explains the print() method, now to your problem...

The problem that your method does not return anything so its a void method, so how can print() "print" anything when you are not passing anything to its method arguements...

The only way it would work is if you returned something from your method, like for example...

in this example it returns a string, so in the method body you need to return a String...

So this should give you an idea on how to fix the problem...
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Davies:

whats the problem with just using public Uselist(){...} and why am i getting this error ?

cheers

chris


Liam has already explained why you are getting the error and given you a pointer on how to fix this.

Now for the other question you ask:
Every method has a signature. Part of the signature is the return type. Every method's signature must have a return type. If the method doesn't return anything, you declare the method's return type to be 'void'. If it does return something, you must tell the compiler what type of data to the method will return. public Uselist(){...} has an incomplete method signature because it is missing the return type; that is what is wrong with it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!