Tim Cooke wrote:I don't really get your question. Can you give an example of what you don't understand about return types?
Piyush Chaudhary wrote:So what does return matters? Why don't we make all void type method ?
Tim Cooke wrote:Try it. What happens when you change the return type to void in your code?
A compile error right?
Say you wanted to write a method that added two int values together. How would you get the result?
Tim Cooke wrote:Yes, but that's because you changed the return type to void and removed the return statement.
fred rosenberger wrote:Try writing a method getName for your Dog class. Have it be void. Create an instance of your Dog class, and set the name. Oh, and a good practice would be to make the name member private.
How would you get the name OUT of the class?
Now change your getName method to return a String.
Piyush Chaudhary wrote:
public void getname(String name)
Bear Bibeault wrote:Of course it matters. A getter named getName will get a name, a setter named setName will set the name.
Without a return type, how could the getName method return the name?
Piyush Chaudhary wrote:
But I have a Liitle doubt that without declaring return how the following code get the name..
Sachin Tripathi wrote:I will try to explain you in simple way
Do one thing make your field private as:
private string name;
Now another class implementing your class X as :
class Y extends X
If you want to give read only access of your field to class Y
You will have to make a getter method in X which [returns value of your field]
I hope it'll help
Sorry for not adding code tags
Sachin Tripathi wrote:In your code you are doing two things wrong(not expected)
1- your getname(String name)
is actually setting(value) field of class
So it should be a setter method than getname
2-why are you printing dog's name?
When you invoked getname(String name) method ,you yourself passed the dog's name .So you should devote setter method for only setting up the field
Tim Harris wrote:There are lots of good resources for understanding return types, but I will try to summarize it here.
The return type of a method states what kind of value it gives back when it is called. Let's take a look at Java's main method:
The return type of the above method is void. That means that, when the method is finished, it does not return any type at all! This is what you use when you don't really need to return a specific value.
But, let's say you want to make a method that adds two numbers together. Just for giggles. Your code might look something like this:
Simple code, but that's not the point - instead of returning nothing, the code is now returning an integer. So how can we use this? We could call it in our main method:
Declaring the type of return ensures safety between methods - making sure that add returns a type of integer means that the main method can reliably treat the result that comes from that method as an integer. It might not seem like that big of a deal when using primitive types like integers, but it definitely comes in handy when dealing with more complex classes, such as Collections!
To address the other issue that has popped up, there are typically some "best practices" concerned when it comes to a classes's variables. Typically, they are all declared private and given special methods if access is necessary. Those special methods are called getters and setters.
Getters are used to fetch information out of a class - in other words, get that information. So, to get the name of your dog, the method might look like this:
Note that return type is a String, because that is what your private variable is! This is considered a safe way to get information about your variables.
Setters, on the other hand, are methods that are used to change the value of a variable - to set the new information. So, if you wanted to change the name of your dog, the method might look something like this:
Now the return type is void, because we don't need a response from the Dog class - we just need to change the name of the dog.
These conventions may seem arbitrary - and in fact, they are. You're absolutely right in that a method named getName can be used to set the name of the Dog - the method names do not mean anything special. But following common conventions makes your code more readable, which in turn makes it easier for you and other programmers to go through and find errors in your code, as well as easier to refactor and expand on.
Campbell Ritchie wrote:Somebody seems to have put a lot of effort into their answer and deserves a cow
Sachin Tripathi wrote:
make setname() private
And try accessing it inside Dog class
And keep getname() public .
Use getname() outside Dog class to see the name assigned by setname().
Sachin Tripathi wrote:Do following things:
1-Make a main method in Dog class
2-in main create instance of Dog class
by new operator
3-invoke setname method as:
and at runtime execute Dog class
By java Dog
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton