• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return Type  RSS feed

 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand the use of return type in Java? Is that really matters to having some return value? Because I did check in some programs, It does not showing any effect..
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really get your question. Can you give an example of what you don't understand about return types?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only return variables that are asignable to the return type, so the return type certainly does make a difference. There are programming languages that don't require type declarations so in that wider sense they aren't 'necessary', but in Java they are necessary because the JLS says it needs to be there.
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:I don't really get your question. Can you give an example of what you don't understand about return types?


Ya, Sure. Why Not. Let the code is following:
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's your question about that code?
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return type of method bark().
It does not showing any effect while we make it int or void?
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While it is compulsory to define a return type for a method, it is not compulsory that you do anything with the return value when calling it. In this case you ignore the return value.
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what does return matters? Why don't we make all void type method ?
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush Chaudhary wrote:So what does return matters? Why don't we make all void type method ?

Because sometimes you want a method to return a value, so that you can use that value for further processing in your program.

In the code you show above, the return value of the bark() method is ignored. In that case you can just as well make the bark() method void, and remove the return 40; statement from the method (line 11).

But just because in your example you don't need to use the return value of the bark() method, does not mean in general that you never need any method to return any value...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


No it,s not giving me compile error..


But thanks I think I will find my answer after further studying Java..
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but that's because you changed the return type to void and removed the return statement.

Plenty of good answers have been given in this thread so far. What is it exactly that you're still unsure of?
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Yes, but that's because you changed the return type to void and removed the return statement.

No, sir. I had mentioned earlier, that while changing its return type does not showing any effect. Its compiling as well as running too..
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

I got your matter what you want to say.. thanks.. While compiling following code the error of privacy is coming.. thanks for your help..
But I have a Liitle doubt that without declaring return how the following code get the name..
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush Chaudhary wrote:
public void getname(String name)

Why would you name a setter with the verb get?
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why would you name a setter with the verb get?
I think it doesn't so much matter in this case, Let change the code with
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will try to explain you in simple way
Do one thing make your field private as:
class X
{
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



 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


Try this,
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush Chaudhary wrote:
But I have a Liitle doubt that without declaring return how the following code get the name..


you're not GETTING the name. Your passing the name INTO another method, and that method is printing it out (and also setting that name).

But all that aside....I have a Kennel class that will hold dogs. I add your dog to my Kennel. I now want to know your dog's name. How can I get it's name? We need a method that returns a String to me so I can know what to call your dog.
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:I will try to explain you in simple way
Do one thing make your field private as:
class X
{
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





I had tried a lot, But unable to understand the meaning, Please help me..
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


Is this right...
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your getter should not take any parameter.
And by convention those methods should be named getName and setName.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of a "getter" as asking a question of an object. examples in the real world would be:

  • What is your name?
  • How old are you?
  • What is your favorite color?



  • in each case, after I ask the question, I am expecting you to give me some answer. And generally, i have an idea what kind of thing your answer will be. Your name will be a string of characters. Your age will be a number. Your favorite color will be a color.

    If I ask you "how old are you", and you try replying with something like "yellow", we have a problem. We are not communicating correctly.

    I can even ask you to do something complicated: "Please go to the store, buy some paint, paint the bedroom of my house, and tell me how much money I owe you". I may have to give you information when I ask - like my address, what color I want, and which bedroom. I am expecting you to return something back to me - in this case, a monetary amount. Again, if you hand me your pet goldfish, something is wrong.

    I can also ask you to do something, and not care about you telling me anything. "Go wave 'hi' to that person over there". that is something you can do, but you don't really need to tell me if you did or not.

    Java is the same way. When I call a method, I'm asking the object to do something. I may have to give the method additional information. It may send something back to me, and if so, we have to agree on what that thing will be. Or, the method may do something and NOT need to tell me anything back.
     
    Liutauras Vilda
    Sheriff
    Posts: 4928
    334
    BSD
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    These graphics might will help to see better, what you setting and what getting.
     
    Piyush Chaudhary
    Ranch Hand
    Posts: 43
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.


    Thanks Tim Harris... I did make it.
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yup ,it seems you understood the concept well.Now variable n will have Jacky.


    To grasp it more firmly 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().

    Try to find its advantage
     
    Piyush Chaudhary
    Ranch Hand
    Posts: 43
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't know how to access the method under that same class?
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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:
    new Dog().setname("jacky");
    and at runtime execute Dog class
    By java Dog
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Somebody seems to have put a lot of effort into their answer and deserves a cow
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You should always use specific names
    CR.You know it always excites me ,everytime i see someone getting a cow(still waiting for mine first ,someday ill surely earn it)
     
    Tim Harris
    Ranch Hand
    Posts: 57
    3
    Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Somebody seems to have put a lot of effort into their answer and deserves a cow


    My parents taught me if you do something, it's worth doing right. So I try my best. Thanks Campbell!
     
    Piyush Chaudhary
    Ranch Hand
    Posts: 43
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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:
    new Dog().setname("jacky");
    and at runtime execute Dog class
    By java Dog


    How can I use this-And keep getname() public .
    Use getname() outside Dog class to see the name assigned by setname().
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Taking example of your last code
    In your Dog class
    in its main method,do this
    1-Dog d=new Dog();
    2-Dogs2 e=new Dogs2(d);
    3-d.setname("jacky");

    Now in the constructor of Dogs2 ,do this:
    Dogs2(Dog d)
    {
    System.out.println(d.getname());
    }

    This will return jacky,that you had assigned in Dog class through setname


    This is the way of letting other class to have readonly access to your class(as they can only see the fields,but can't set it,
    As you can't invoke setname method inside Dogs2)
     
    Piyush Chaudhary
    Ranch Hand
    Posts: 43
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Is this what you are saying!!
     
    Liutauras Vilda
    Sheriff
    Posts: 4928
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Look carefully at lines 22, 23, 24.
    What is the dog "d" name at line 23?
     
    Piyush Chaudhary
    Ranch Hand
    Posts: 43
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!