LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-09-2005, 10:54 PM   #1
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Rep: Reputation: 30
java pass created class by refference


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...

is there a way i can pass an object by reference?
 
Old 01-09-2005, 11:19 PM   #2
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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.

Last edited by wapcaplet; 01-09-2005 at 11:20 PM.
 
Old 01-10-2005, 02:02 AM   #3
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
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.
 
Old 01-10-2005, 07:16 PM   #4
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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):

Code:
snake.moveSnake( map );

Last edited by wapcaplet; 01-10-2005 at 07:18 PM.
 
Old 01-10-2005, 10:33 PM   #5
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
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.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running a Java executable class from another executable class LUB997 Programming 22 07-24-2005 04:57 AM
Pass object variable to anothor class in JAVA hus Programming 2 05-11-2005 04:04 AM
how pass a class to a function aditya1 Programming 2 03-08-2005 09:02 PM
Java Question on using your own class k1ll3r_x Programming 2 01-22-2005 10:11 AM
Compile Java - .class, .java, .jar ? woranl Programming 2 11-09-2004 10:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration