Sythe.Org Forums     Register     FAQ     Members List     Calendar     Mark Forums Read    
 
Sythe.Org Forums  
   Runescape Gold

Sythe.org — A Virtual Goods Trading Hub

Make real cash! buying and selling in-game items.

We have a no-scam policy.

You can make thousands playing your favourite games here at Sythe.org.

Just sign up an account and follow the rules!


Take me to

Runescape Markets

Other Game Markets

Support Center

Register an Account

Close
[JAVA] reutrn, set, get
Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 06-16-2010, 08:31 PM
Member
 
Join Date: May 2007
Posts: 85
Default [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());	
	}
}

Last edited by Lol Omg dude : 06-16-2010 at 08:32 PM.
Reply With Quote
  #2  
Old 06-16-2010, 09:37 PM
Guru
 
Join Date: Jun 2005
Location: Canada
Posts: 1,844
Default Re: [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.
__________________

Reply With Quote
  #3  
Old 06-16-2010, 09:54 PM
Member
 
Join Date: May 2007
Posts: 85
Default Re: [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.
Reply With Quote
  #4  
Old 06-16-2010, 11:05 PM
Guru
 
Join Date: Jun 2005
Location: Canada
Posts: 1,844
Default Re: [JAVA] reutrn, set, get

Quote:
Originally Posted by Lol Omg dude View Post
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.
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 int getInteger() { return count;  }
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.
__________________


Last edited by blindkilla : 06-16-2010 at 11:06 PM.
Reply With Quote
  #5  
Old 06-16-2010, 11:48 PM
Member
 
Join Date: May 2007
Posts: 85
Default Re: [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.
Reply With Quote
  #6  
Old 06-17-2010, 12:04 AM
Guru
 
Join Date: Jun 2005
Location: Canada
Posts: 1,844
Default Re: [JAVA] reutrn, set, get

Quote:
Originally Posted by Lol Omg dude View Post
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 accessed by the orange class so if I want to use it in the apple class I have to declare two functions in the orange class that can get and set that variable.
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.
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.

Quote:
Originally Posted by Lol Omg dude View Post
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.
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.
__________________

Reply With Quote
  #7  
Old 06-17-2010, 02:41 AM
Member
 
Join Date: May 2007
Posts: 85
Default Re: [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.

Last edited by Lol Omg dude : 06-17-2010 at 02:41 AM.
Reply With Quote
  #8  
Old 06-17-2010, 11:35 AM
Guru
 
Join Date: Jun 2005
Location: Canada
Posts: 1,844
Default Re: [JAVA] reutrn, set, get

Quote:
Originally Posted by Lol Omg dude View Post
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.
Exactly, they are just what you name your functions.
__________________

Reply With Quote
  #9  
Old 06-19-2010, 04:44 AM
Legend
Java Programmers Pirate
 
Join Date: Jan 2007
Posts: 10,887
<3 n4n0
Default Re: [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. :/
__________________
PM me with any issues.

I do not MM or trade anything.
Anyone claiming to be me in a trade or MM is an imposter.
Reply With Quote
  #10  
Old 06-19-2010, 11:05 PM
Guru
 
Join Date: Jun 2005
Location: Canada
Posts: 1,844
Default Re: [JAVA] reutrn, set, get

Quote:
Originally Posted by SuF View Post
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,
That's bad programming, class variables should always be private.

Quote:
Originally Posted by SuF View Post
just as you can not change the variable or you can not read it. :/
I don't know what you're trying to say here.
__________________

Reply With Quote
  #11  
Old 06-19-2010, 11:22 PM
Legend
Java Programmers Pirate
 
Join Date: Jan 2007
Posts: 10,887
<3 n4n0
Default Re: [JAVA] reutrn, set, get

Quote:
Originally Posted by blindkilla View Post
That's bad programming, class variables should always be private.
Why is it bad programming?
__________________
PM me with any issues.

I do not MM or trade anything.
Anyone claiming to be me in a trade or MM is an imposter.
Reply With Quote
  #12  
Old 06-19-2010, 11:34 PM
Guru
 
Join Date: Jun 2005
Location: Canada
Posts: 1,844
Default Re: [JAVA] reutrn, set, get

Quote:
Originally Posted by SuF View Post
Why is it bad programming?
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.
__________________

Reply With Quote
  #13  
Old 06-20-2010, 01:24 AM
Legend
Java Programmers Pirate
 
Join Date: Jan 2007
Posts: 10,887
<3 n4n0
Default Re: [JAVA] reutrn, set, get

Quote:
Originally Posted by blindkilla View Post
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.
What? You have no control with private variables? Explain please.
__________________
PM me with any issues.

I do not MM or trade anything.
Anyone claiming to be me in a trade or MM is an imposter.
Reply With Quote
  #14  
Old 06-20-2010, 01:38 AM
Guru
 
Join Date: Jun 2005
Location: Canada
Posts: 1,844
Default Re: [JAVA] reutrn, set, get

Quote:
Originally Posted by SuF View Post
What? You have no control with private variables? Explain please.
I said you have no control without private variables.
__________________

Reply With Quote
  #15  
Old 06-20-2010, 02:27 AM
Legend
Java Programmers Pirate
 
Join Date: Jan 2007
Posts: 10,887
<3 n4n0
Default Re: [JAVA] reutrn, set, get

Quote:
Originally Posted by blindkilla View Post
I said you have no control without private variables.
Yes you do. Public variables means you have all the control you want.
__________________
PM me with any issues.

I do not MM or trade anything.
Anyone claiming to be me in a trade or MM is an imposter.
Reply With Quote
  #16  
Old 06-20-2010, 03:52 AM
Guru
 
Join Date: Jun 2005
Location: Canada
Posts: 1,844
Default Re: [JAVA] reutrn, set, get

Quote:
Originally Posted by SuF View Post
Yes you do. Public variables means you have all the control you want.
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.
__________________

Reply With Quote
  #17  
Old 07-07-2010, 12:35 AM
Forum Addict
 
Join Date: Feb 2007
Posts: 306
Default Re: [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.
Reply With Quote
  #18  
Old 07-07-2010, 02:46 PM
Member
 
Join Date: May 2007
Posts: 85
Default Re: [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.
Reply With Quote
Reply



Cheap RS Gold Store  Runescape Gold

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

All times are GMT +1. The time now is 06:12 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.6.1