[JAVA] reutrn, set, get

Discussion in 'Programming General' started by Lol Omg dude, Jun 16, 2010.

[JAVA] reutrn, set, get
  1. Unread #1 - Jun 16, 2010 at 3:31 PM
  2. Lol Omg dude
    Joined:
    May 16, 2007
    Posts:
    85
    Referrals:
    0
    Sythe Gold:
    0

    Lol Omg dude Member

    [JAVA] reutrn, set, get

    Ok I don't understand how the commands return, set, get work on Java with multiple methods and instances. Can someone explain to me what is going on exactly?

    Here's a sample code:


    apples.java
    Code:
    import java.util.Scanner;
    
    class apples {
    	public static void main (String args[]){
    		Scanner input = new Scanner(System.in);
    		oranges orangesObject = new oranges ();
    		System.out.println("Enter name here: ");
    		String temp = input.nextLine();
    		orangesObject.setName(temp);
    		orangesObject.saying();
    		
    		
    	}
    }
    

    oranges.java
    Code:
    
    public class oranges {
    	private String personName;
    
    	public void setName(String name)
    	{
    	personName = name;	
    	}
    	
    	
    	public String getName()
    	{
    	return personName;	
    	}
    	
    	
    	public void saying()
    	{
    	System.out.printf("The name is %s", getName());	
    	}
    }
    
     
  3. Unread #2 - Jun 16, 2010 at 4:37 PM
  4. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    [JAVA] reutrn, set, get

    Basically, in a class you have public and private members.

    Private members cannot be directly accessed from outside the class. They can only be accessed from inside the class.

    So if you want to set the value for a private variable, or get the value in a private variable you need to make a function to do it for you.

    Code:
    public class oranges {
           private:
                 int amountOfOranges;
                 double costPerOrange;
           public:
                 //class constructor
                orange(int numb, double cost) {
                amountOfOranges = numb;
                costPerOrange = cost;
                }
    }
    
    There is your class, not I'm not a java person so syntax may not be correct, but it should be fine to understand the concept.

    Code:
    main() {
           Orange ora = new Orange(12, 2.33);
           
           System.out.printf("amount we have %d", ora.amountOfOranges); //Error
           ora.amountOfOranges = 33; //Error
    }
    
    The above won't work because you cannot access private members directly from outside the class. So that is why we make get/set functions to do it for us from inside the class.

    Code:
    public class oranges {
           private:
                 int amountOfOranges;
                 double costPerOrange;
           public:
                 //class constructor
                orange(int numb, double cost) {
                amountOfOranges = numb;
                costPerOrange = cost;
                }
    
                public void setOrangesAmount(int x) {
                amountOfOranges = x;
                }
    
                public int getOrangesAmount(){
                return amountOfOranges;
                }
    }
    
    We can use the two new functions above, to change the value of the variable amountOfOranges.

    Code:
    main() {
           Orange ora = new Orange(12, 2.33);
           
           System.out.printf("amount we have %d", ora.getOrangesAmount()); 
           ora.setOrangesAmount(33); 
    }
    
    If you need further explanation let me know, I know mine probably wasn't good.
     
  5. Unread #3 - Jun 16, 2010 at 4:54 PM
  6. Lol Omg dude
    Joined:
    May 16, 2007
    Posts:
    85
    Referrals:
    0
    Sythe Gold:
    0

    Lol Omg dude Member

    [JAVA] reutrn, set, get

    So basically the set and get functions just retrieve/place stuff that I can't access?

    So for example:

    I have a private orange and I put that in a box :p and you can't access it because you're in a different class. So in order for me to have you access that orange I have to use the function [get]. Is that right?

    I'm browsing through the Sun Tutorials but I can't find Set,Get, Return anywhere.

    Also..
    How do I know when I should use the [return] function? Like what types of commands expects a return?

    I'm using these tutorials but in this particular video he did not explain it very well.

    http://www.youtube.com/watch?v=9t78g0U8VyQ

    Thanks for your reply, means a lot.
     
  7. Unread #4 - Jun 16, 2010 at 6:05 PM
  8. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    [JAVA] reutrn, set, get

    I don't understand what you mean by the orange in the box. Do you mean an instance of an orange class in a class named box?

    If that is the case then you could only access the public functions and variables in the orange object, you would not be able to access any private members.

    Get and set are just more or less nicknames for functions which describe their purpose.

    Get functions get (return) a value.
    Set functions set (assign) a value.

    Here is an example of a get function:
    Code:
    public [B]int[/B] getInteger() { return [B]count[/B];  }
    The int specifies that the function will be returning an integer. The count would in that case be a private integer in your program.


    Read the first little bit of this page (http://www.cplusplus.com/doc/tutorial/classes/), it might give you a better understanding than my post on how classes and access modifiers work. It's for c++ but the idea is the same.
     
  9. Unread #5 - Jun 16, 2010 at 6:48 PM
  10. Lol Omg dude
    Joined:
    May 16, 2007
    Posts:
    85
    Referrals:
    0
    Sythe Gold:
    0

    Lol Omg dude Member

    [JAVA] reutrn, set, get

    It'll probably be better if I just tell you how I interpret it and then you can correct me and maybe explain it in a way I understand.

    When I see
    Code:
    private String personName
    
    I say, Ok this variable can only be used by the orange class so if I want to use it in the apple class I have to declare a variable for that in the apple class.

    When I see
    Code:
    public void setName(String name)
    	{
    	personName = name;	
    	}
    
    I don't know where setName came from. You said that set and get are just nicknames which describe a purpose so where does the 'Name' come from? I don't think I declared a variable for it.

    When I see
    Code:
    public String getName()
    	{
    	return personName;	
    	}
    
    All I know is that I declared a public variable called getName. I don't quite understand how 'return personName' plays into this.

    Sorry if I'm a little thick I just don't fully understand what's going on. I read the C++ tutorial link and now I understand how I "linked" both classes. For example: I have apples and oranges classes and for me to use stuff in the oranges classes I made an oranges object called orangesObject in my apples class. So I guess that part is cleared up.
     
  11. Unread #6 - Jun 16, 2010 at 7:04 PM
  12. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    [JAVA] reutrn, set, get

    setName is the name of your function. You might need to look into some more more programming basics before getting into classes.

    Public means it can be accessed outside the function. Void means that the function won't be returning any values. setName is the name you give it, this can be anything you want. Inside the brackes its the value you will be passing to the function from outside the class

    Orange orange = new Orange()
    orange.setName("gary")

    That would send the string gary into name which would be a local variable in that function.

    It would set the value of name (which is gary) to personName.

    return basically returns the value of something

    if i had the function
    Code:
    public int returnTest() { return 22; }
    
    int test = 0;
    
    test = returnTest;
    
    printf(test)  // Output would be 22.
    
    
    When return is used, it returns the value of the item you are returning to where the function was called from. If you use a return function without giving the return value anywhere to go, it will result in an error.
     
  13. Unread #7 - Jun 16, 2010 at 9:41 PM
  14. Lol Omg dude
    Joined:
    May 16, 2007
    Posts:
    85
    Referrals:
    0
    Sythe Gold:
    0

    Lol Omg dude Member

    [JAVA] reutrn, set, get

    Oh I get it now, wow I can't believe i missed this.

    So setName is basically just a name for the function

    Code:
    personName = name
    and I'm guessing getName is also a name for the function or return

    Code:
    return personName 
    I gotcha, so in reality getName and setName really don't mean anything they are just names.
     
  15. Unread #8 - Jun 17, 2010 at 6:35 AM
  16. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    [JAVA] reutrn, set, get

    Exactly, they are just what you name your functions.
     
  17. Unread #9 - Jun 18, 2010 at 11:44 PM
  18. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    [JAVA] reutrn, set, get

    Can I add....

    You could always just make it a public variable and not use the Get/set method way of doing this. You should make things private only if you want to have control of what else as access to it, just as you can not change the variable or you can not read it. :/
     
  19. Unread #10 - Jun 19, 2010 at 6:05 PM
  20. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    [JAVA] reutrn, set, get

    That's bad programming, class variables should always be private.

    I don't know what you're trying to say here.
     
  21. Unread #11 - Jun 19, 2010 at 6:22 PM
  22. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    [JAVA] reutrn, set, get

    Why is it bad programming?
     
  23. Unread #12 - Jun 19, 2010 at 6:34 PM
  24. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    [JAVA] reutrn, set, get

    You already set it yourself, without keeping variables private you have no control within the class. In bigger programs control is essential, that's why you get used to keeping variables private.
     
  25. Unread #13 - Jun 19, 2010 at 8:24 PM
  26. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    [JAVA] reutrn, set, get

    What? You have no control with private variables? Explain please.
     
  27. Unread #14 - Jun 19, 2010 at 8:38 PM
  28. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    [JAVA] reutrn, set, get

    I said you have no control without private variables.
     
  29. Unread #15 - Jun 19, 2010 at 9:27 PM
  30. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    [JAVA] reutrn, set, get

    Yes you do. Public variables means you have all the control you want.
     
  31. Unread #16 - Jun 19, 2010 at 10:52 PM
  32. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    [JAVA] reutrn, set, get

    In the sense of changing its value yes, but not validation wise. If multiple classes are using the same class, you need some sort of validation and error handling in the class to make sure the right values are inputted.

    The value you want to change may also depend on another value, or another value might depend on the value you are adding.

    If you end up just making stuff like this public and hard coding all this validation and logic, and you end up changing something eventually, you will have to go through changing all your code where this stuff was used. But instead, you can just use private variables and have this logic in the class.
     
  33. Unread #17 - Jul 6, 2010 at 7:35 PM
  34. aznguy94
    Joined:
    Feb 11, 2007
    Posts:
    304
    Referrals:
    0
    Sythe Gold:
    0

    aznguy94 Forum Addict

    [JAVA] reutrn, set, get

    First, I hope you understand the difference between a class and an object. The class is the template for which the object is made; the class is the blueprint. The object is the building which is made from the blueprint. Multiple buildings (objects) can be made from the same blueprint (class). Every object contains the same variables and methods although the variables of each object usually have different values for their variables.

    You build objects of a class using the new operator in Java. For example, in your code, you say

    oranges orangesObject = new oranges ();

    You created an object called orangesObject from the template (class) oranges. Objects are instances of a class; they have the same outline as other objects of a class but can differ in the values of their variables.

    In classic Java Object Oriented Programming, the methods of a class are public while the variables are private. Private variables mean that when you create an object of the class, those variables can only be accessed within that object and not by any other objects.

    There are two general types of methods within a class: accessor and mutator methods. Accessor methods are used to access variables inside an object. Mutator methods are used to change variables inside an object. Usually, accessor methods are named getXXXX because they get or access a variable in an object of a class. Mutator methods are named setXXXX because they set or change a variable in an object of a class. Since the variables are private and the methods are public, accessor and mutator methods are necessary in Object Oriented Programming because the methods can be accessed outside the object while the variables can't be.

    I hope i made it clearer at all, or if this post is too late I wasted a lot of time.
     
  35. Unread #18 - Jul 7, 2010 at 9:46 AM
  36. Lol Omg dude
    Joined:
    May 16, 2007
    Posts:
    85
    Referrals:
    0
    Sythe Gold:
    0

    Lol Omg dude Member

    [JAVA] reutrn, set, get

    It's never too late. The more I hear it explained in different ways the better I understand it. So thank you for your efforts.
     
< Help with CSS & HTML | i need sony vegas! >

Users viewing this thread
1 guest


 
 
Adblock breaks this site