• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

return type of method

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

in the code above, line 16 don't compile because if a method doesn't return a value, we can't assign the result of that method to a variable. But setWeight method
is also void and line 3 compiles without any errors.  i can't understant why line 16 doesn't compile while line 3 compiles succesfully
 
Ranch Hand
Posts: 543
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why wouldn't line 3 not compile?

all you are doing is creating a method with a double as an argument then you are assigning weight to value,completely legal

where as line 16 will not compile because newWeight is a double and the method setWeight does not return anything,if you wanted this to work set the return type as double and return the value weight,

hope that makes sense.
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you code stands right now you have one issue copied twice

In order to get the value of weight you would need to use a method name "getWeight" or something like that. With this new method you can return the value of the weight variable.
void can/should be thought of a nothing. On line 13 you are trying to give a value of nothing (void) to the newWeight variable which you told the system would always be a double.
Usually when staring out with a project your get methods (getters) and set methods (setters) will be only a few lines of code.
 
Faig Huseynov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your response
 
Everybody's invited. Even this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic