ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i have created a class called snakeMap, and have an instance called map. i have another class called snakeObj. when i create a snakeObj, i need to pass it an instance of snakeMap, but i need to pass the map by reference, so it will change globally...
There really is no "pass by reference" in Java; references to objects are indeed passed to functions, but they are passed by value. If you change the object reference itself inside a function, it will be lost when the function exits. You can change class data, however (using member functions), and the data will be retained. Check out this explanation.
yes, i understand all of that. i was just wondering if it were possible to pass by reference.... anyways, maybe someone has an idea on how i can do this then...
my problem is that in a class snakeMap, there is an array of int's that form the actual map. there is also a class snakeObj that needs to edit that array in snakeMap. both snakeMap and snakeObj have 1 instance created in a class called snakeWindow. snakeMap and snakeObj must both be created in snakeWindow because snakeObj needs something to be passed from snakeWindow. how can i get snakeObj to edit the array in snakeMap?
these code parts may help...
snakeMap needs to be created in snakeWindow because it needs to be psased pen, which can only be found in paint method.
snakeObj needs to be created in snakeWindow because snakeWindow is where the key presses are found.
snakeMap and snakeObj both need to be able to edit the array in map
Code:
snakeWindow:
private snakeMap map;
private snakeObj snake;
public void paint(Pen pen){
map.drawMap(pen);
}
snakeObj:
public snakeObj(snakeMap m, int init_x, int init_y){
...
}
public void moveSnake(){
//edits m depending on snake movement
}
Last edited by true_atlantis; 01-10-2005 at 02:54 AM.
All you'd need to do, it seems, is pass in the snakeMap (from snakeWindow) to whatever function needs it. So, for example, since you have the snakeObj constructor that takes a snakeMap argument, you could call the function like this (from within snakeWindow):
Code:
snakeObj foo( map, 0, 0 );
The snakeObj constructor could then do whatever it likes with the map data, and the changes will be retained. In your other function moveSnake, declare the function to receive a snakeMap argument:
Code:
public void moveSnake( snakeMap m ) {
//edits m depending on snake movement
}
Then just call it with the map as an argument (again, from within snakeWindow):
i am a little confused... are you saying that is hould pass the snakeMap in moveSnake instead of passing it in the constructor? even if i do that, i dont think it will work... this is what i need to happen... i need the snakeWindow class to call a method in the snakeObj class. the snakeObj class needs to chagne in instance of snakeMap that was created in snakeWindow... i havent tried your suggestion... but i will sometime soon.. thanks
JUST TRIED IT... THANKS, works perfect
Last edited by true_atlantis; 01-10-2005 at 10:36 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.