Re: Pass by Value Problem
Quote:
the showID integer to be passed over to Cost class using method at the bottom of panel class
So to make sure I have this straight, you want a value from the Panel class to be passed to the Cost class. If so, then you seem to be a bit confused about what your methods are doing.
Code:
public void getnum(int num1)
{
num = num1;
}
You have named this method getnum, but really you should have named it setnum. The method takes an integer named num1 as a parameter, and it makes the class variable named num equal to that value. It doesn't "get" any values at all, it sets one.
Instead, you want a method that returns a value. They keyword "void" that you used means that the method does not return a value, which is not what you want. Instead, you want the method to return an int. You need to make several changes to the method to do that.
Code:
public int getNum() //change void to int, no parameters needed
{
return num; //return keyword used to pass a value back
}
Re: Pass by Value Problem
i see why you are thinking like this; you may be incorrectly understanding that the sequence of letters "GET" has some meaning to java; it doesnt. java doesnt care what spoken-language you program in..
youre actually expecting this to work in a pass-by-reference sense of the word. pass by value cannot be used to populate a variable in this context
i'll pick on an example that works as per your expectation:
byte[] buffer = new byte[4096];
inputStream.read(buffer);
the inputStream is a source of bytes. when you pass the buffer into the read method, the read method will fill it up. after it returns, the buffer will be filled with data.
the read() method works this way by design, but it operates on objects. a byte[] is an object, and is hence ALWAYS passed by reference. this is the ONLY reason that doing it like this, can work for a method like read(), because the method argument is an OBJECT.
primitive variables dont work this way; they always pass by value. pass by value means: if you have a variable in memory that is holding the number 3, and you pass it to a method.. then a copy of the number 3 is given to the method. the method can change it as much as it likes, it wont change the original value. like giving a document to your friend on a floppy disk.. he can change it as much as he likes and your copy is unaffected.
primitive variables are always passed this way. remember this. it is one of the unbendable truths about java.
Re: Pass by Value Problem
Hi all,
thanks for taking a look at this and explaining to me about what i need. I have changed my method for returning the value like so:
Code:
public int getnum()
{
return num;
}
which should return the int num which has been given a value set in the code above.
ok so now i have a return method in my panel class, i think i am calling that method incorrectly as it duplicates the whole panel class..not what i want if you catch my drift...
i call it in the cost class like so
Code:
Panel cust;
cust = new Panel();
num1 = cust.getnum();
System.out.println(cust.getnum());
if (num1 > screen1)
{
//do something with num etc
i think this is still wrong because im still getting zero result.
sorry for being a pain in the arse....