Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointers in java? 2

Status
Not open for further replies.

joelwenzel

Programmer
Jun 28, 2002
448
Hi,

I want to make a pass that contains pointers to other objects of that same class (like a linked list)...

In C++ I would use pointers to do this. Am I right saying that in java, I don't have to specifically declare something as a pointer. If I say

myclass bob;
bob.x = 5;

myclass tim = bob;

then tim actually points to bob and is not another instance of myclass that has all the data from bob copied into it?

Thanks
 
There are no naked pointers in Java. All object variables are like pointers in a way, but there is no 'pointer arithmetic' functionality available.
If you assign an object to two different variables, both variables will contain (or 'point') to the same object. The object itself is not copied.

Code:
Myclass bob = new Myclass();
bob.x = 5;
MyClass tim = bob;
bob.x = 7;
System.out.println("x value=" + tim.x);
would print 7 not 5, because both tim and bob 'point' to the same object.

Tim (me, not the instance!)
 
From the Sun site :

Code:
Arrays, classes, and interfaces are reference (in the glossary) types. The value of a reference type variable, in contrast to that of a primitive type, is a reference to (an address of) the value or set of values represented by the variable.

A reference is called a pointer, or a memory address in other languages. The Java programming language does not support the explicit use of addresses like other languages do. You use the variable's name instead.

Click here to learn Ways to help with Tsunami Relief
--------------------------------------------------
Free Database Connection Pooling Software
 
... which is what I said, and nice to have it confirmed by Sun / sedj.[smile]

Tim
 
OK cleverclogs,

Tell me the output of each "System.out" from this class (and no compiling !).

Code:
public class TestTest {

	class SomeClass {
	    public int var = 1;
	}

	public static void main(String[] args) {
		new TestTest().test();
	}

	public void test() {
     		SomeClass one = new SomeClass();
     		one.var = 3;
     		SomeClass two = one;

     		System.out.println("1) one.var(" +one.var +"), two.var(" +two.var +")");


     		testPass(two);
     		System.out.println("2) one.var(" +one.var +"), two.var(" +two.var +")");

     		testPass2(two);
     		System.out.println("3) one.var(" +one.var +"), two.var(" +two.var +")");
	}

	public void testPass(SomeClass sc) {
		sc.var = 9;
	}

	public void testPass2(SomeClass sc) {
		SomeClass sc2 = new SomeClass();
		sc2.var = 4;

		sc = sc2;

		sc2.var = 7;
	}

}

So, fill in the blanks ...

1) one.var(?), two.var(?)
2) one.var(?), two.var(?)
3) one.var(?), two.var(?)

Click here to learn Ways to help with Tsunami Relief
--------------------------------------------------
Free Database Connection Pooling Software
 
..have I rattled your cage, Sedj?

Anyway, without compiling ...

1) one.var(3), two.var(3)
2) one.var(9), two.var(9)
3) one.var(9), two.var(9)

because you can't have pointers-to-pointers.

... I did compile afterwards to check my answer, because I'm not that confident.
 
Hehehe, cage rattling :)
I am embarrassed to say that for a while I thought it would be :

1) one.var(3), two.var(3)
2) one.var(9), two.var(9)
3) one.var(7), two.var(7)

until I remembered (or rather my colleague reminded me :p ) that Java passes a copy of the pointer to an object - not the actual pointer itself, to methods ... thus rendering no effect on the original object. Sometimes I think pointers in Java are more tricky than in C :)




Click here to learn Ways to help with Tsunami Relief
--------------------------------------------------
Free Database Connection Pooling Software
 
I think you're right, Sedj. I too have a bit of C under my belt, and do find myself sometimes having to sit and think when passing objects around in Java.
Thanks for the little exercise, anyway :)

Tim
 
ok, this is what I thought...java automatically does pointers. Thanks for your help! Stars all around :)
 
looks like I already kneow you would be answering this post before you did timw

"Tim (me, not the instance!)" hehe
 
Actually, you guys might be able to help with one other problem I'm having...I think it is also a pointer problem

int tim;

public void sedj(){

for(int i=0;i<5;i++)
{
obj.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
...
tim= i;
...
}
});
}
}

I get an error on the tim=i line. I think it is because i is going to be erased after this function has been executed. But I want each obj in obj[] to set tim = i for index i. How do I do this...must I do something like

tim = new Integer(i);
 
I don't think so.

Your tim variable is out of the scope of the MouseAdapter class. You cannot reference non final variables inside an inner class.

Cheers,

Dian
 
tim and i are primitives, and arn't pointers. tim doesn't exsit in a mouseAdapter. Fix is to add a constructor which takes something that wraps an int... Which gets messy.

Design questions to ask yourself:
What is tim supposed to be?
What object is it more closely belong the mouseAdapter or object with the sedj method?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top