<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: Java pointers</title>
	<link>http://ask.metafilter.com/25482/Java-pointers/</link>
	<description>Comments on Ask MetaFilter post Java pointers</description>
	<pubDate>Thu, 13 Oct 2005 14:53:04 -0800</pubDate>
	<lastBuildDate>Thu, 13 Oct 2005 14:53:04 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: Java pointers</title>
		<link>http://ask.metafilter.com/25482/Java-pointers</link>	
		<description>Very specific JAVA question. Java.sun.com and various google searches have been of no use.  &lt;br /&gt;&lt;br /&gt; I&apos;ve written two classes, which for simplicity sake I&apos;ll call class A and class B. I have an instance of class A, which I&apos;ll call a. Within a are a couple of instances of class B, which I&apos;ll call b and bb. b and bb need to be able to access variables and methods in a. Not A, a, so making everything static is not really an option. In C++ I would use a pointer, but this isn&apos;t C++ and Java is annoying like that. Any suggestions on how I can make this work?</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2005:site.25482</guid>
		<pubDate>Thu, 13 Oct 2005 14:45:59 -0800</pubDate>
		<dc:creator>martinX&apos;s bellbottoms</dc:creator>
		
			<category>Java</category>
		
			<category>programming</category>
		
	</item> <item>
		<title>By: andrew cooke</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402440</link>	
		<description>have you considered making B an inner class of A?&lt;br&gt;
&lt;br&gt;
if you post the C++ code here it would make it easier to give a solution.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402440</guid>
		<pubDate>Thu, 13 Oct 2005 14:53:04 -0800</pubDate>
		<dc:creator>andrew cooke</dc:creator>
	</item><item>
		<title>By: R343L</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402442</link>	
		<description>First, this sounds broken. Two classes mutually needing to use each other is just .. weird (not even sure Java syntactically would allow the mutual imports...)&lt;br&gt;
&lt;br&gt;
Having said that, you could probably use reflection to dynamically access the other object (and just store a vanilla Object type reference of &apos;a&apos; on b).&lt;br&gt;
&lt;br&gt;
Not that  I recommend that.&lt;br&gt;
&lt;br&gt;
Rachael</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402442</guid>
		<pubDate>Thu, 13 Oct 2005 14:55:12 -0800</pubDate>
		<dc:creator>R343L</dc:creator>
	</item><item>
		<title>By: R343L</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402446</link>	
		<description>Oooh. Should have previewed. I forgot about inner classes. Thinking a bit more in the usage of other mainstream languages. That would probably work pretty well (and doesn&apos;t seem like as much of a hack!)&lt;br&gt;
&lt;br&gt;
Rachael</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402446</guid>
		<pubDate>Thu, 13 Oct 2005 14:57:46 -0800</pubDate>
		<dc:creator>R343L</dc:creator>
	</item><item>
		<title>By: Rothko</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402447</link>	
		<description>I&apos;m having difficulty parsing what you want to do; perhaps you&apos;re referring to &lt;a href=&quot;http://www.javaworld.com/javaworld/javatips/jw-javatip30.html&quot;&gt;polymorphism&lt;/a&gt;.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402447</guid>
		<pubDate>Thu, 13 Oct 2005 14:58:29 -0800</pubDate>
		<dc:creator>Rothko</dc:creator>
	</item><item>
		<title>By: smackfu</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402449</link>	
		<description>I don&apos;t get why you think you can&apos;t use a pointer.  Just pass the object (or &lt;i&gt;this&lt;/i&gt; if you&apos;re in the object already).</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402449</guid>
		<pubDate>Thu, 13 Oct 2005 15:01:43 -0800</pubDate>
		<dc:creator>smackfu</dc:creator>
	</item><item>
		<title>By: knave</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402451</link>	
		<description>Yeah, you have a design issue here, called a cyclic dependency.  Usually avoidable, but not always.&lt;br&gt;
&lt;br&gt;
In Java, you can pass your object into the b objects easily.  Make them so their constructors take an &quot;A&quot; parameter, and pass in &quot;this&quot; (inside &apos;a&apos;, which is a reference to &apos;a&apos; itself) when you create them.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402451</guid>
		<pubDate>Thu, 13 Oct 2005 15:05:48 -0800</pubDate>
		<dc:creator>knave</dc:creator>
	</item><item>
		<title>By: orthogonality</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402465</link>	
		<description>He&apos;s using the B instances as &lt;i&gt;closures&lt;/i&gt;.&lt;br&gt;
&lt;br&gt;
Couple of possibilites: do as Andrew Cooke suggests and make B an inner class of A. &lt;br&gt;
&lt;br&gt;
Or make B an interface and the instances anonymous classes that implement the interface. &lt;br&gt;
&lt;br&gt;
Or, if B only needs access to A&apos;s public interface, do what smackfu suggests: make the B ctor take an A instance, and operate on its A instance through that reference.&lt;br&gt;
&lt;br&gt;
For more on closures in java, see &lt;a href=&quot;http://mindprod.com/jgloss/closure.html&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;http://www.kataba.com/weblog/Entry.asp?blog=Chris&amp;entry=2004-08-31-1-closure_idiom&quot;&gt;here.&lt;/a&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402465</guid>
		<pubDate>Thu, 13 Oct 2005 15:25:29 -0800</pubDate>
		<dc:creator>orthogonality</dc:creator>
	</item><item>
		<title>By: martinX&apos;s bellbottoms</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402481</link>	
		<description>smackfu and knave: to my understanding java doesn&apos;t work that way. If I pass a as a reference to B&apos;s constructor, I&apos;ll end up with a copy of a, not a. Any changes I make will be to the copy, not the original a. The C++ code to declare b would look something like B b=new B(&amp;amp;a); because the ampersand passes the address of a rather than creating a new instance of A that at the time is the same as a. Now I&apos;m probably mixing C++ and Java, but anyone who knows both will get the point. &lt;br&gt;
&lt;br&gt;
I&apos;ll try making B an inner class in A, and see if that works out the way I want it to. B is eventually going to become a thread, and A extends JPanel, from the java.awt library, and the method I most need to access is the paint method, if that makes any difference.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402481</guid>
		<pubDate>Thu, 13 Oct 2005 15:43:30 -0800</pubDate>
		<dc:creator>martinX&apos;s bellbottoms</dc:creator>
	</item><item>
		<title>By: fnerg</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402488</link>	
		<description>Doing something like&lt;br&gt;
&lt;br&gt;
public class A {&lt;br&gt;
  private B b = new B(this);&lt;br&gt;
  private B bb = new B(this);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
passes a reference along to b and bb. No extra copies made.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402488</guid>
		<pubDate>Thu, 13 Oct 2005 15:52:28 -0800</pubDate>
		<dc:creator>fnerg</dc:creator>
	</item><item>
		<title>By: sbutler</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402491</link>	
		<description>First, passing objects around in Java is always by reference. You won&apos;t make a copy unless you explicitly do so (primatives are another matter). This is similar to C++ if you had &quot;class B { private A a; B( A&amp;amp; _a ) : a( _a ) {} };&quot;.&lt;br&gt;
&lt;br&gt;
But you really want an inner class here. If I do:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;public class A {&lt;br&gt;    private class B extends Thread {&lt;br&gt;        public void run() {&lt;br&gt;            bar();&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    B b;&lt;br&gt;&lt;br&gt;    public void foo() {&lt;br&gt;        b = new B();&lt;br&gt;        b.start()&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    public void bar() {&lt;br&gt;        // do something&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;
&lt;br&gt;
That will work how you&apos;d expect it to.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402491</guid>
		<pubDate>Thu, 13 Oct 2005 15:56:27 -0800</pubDate>
		<dc:creator>sbutler</dc:creator>
	</item><item>
		<title>By: true</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402494</link>	
		<description>Java is all pointers. They aren&apos;t called pointers, but unless you make an explicit copy yourself or are talking about primitives (int, long ...) or String you&apos;re deailing with pointers.&lt;br&gt;
&lt;br&gt;
If you pass A in B&apos;s constructor it will be &quot;the&quot; A, not a copy of A. Best would be to have A implement an interface of the methods it chooses to vend to B and use that as the type of the object in the constructor to B.&lt;br&gt;
&lt;br&gt;
Example&lt;br&gt;
&lt;pre&gt;&lt;br&gt;
public class A implements UsefulInterface {&lt;br&gt;
public A() {&lt;br&gt;
    B b = new B((UsefulInterface)this); // The cast is for clarity&lt;br&gt;
    B bb = new B((UsefulInterface)this);&lt;br&gt;
 }&lt;br&gt;
}&lt;br&gt;
&lt;/pre&gt;&lt;br&gt;
The constructor for B looks like &lt;br&gt;
&lt;pre&gt;&lt;br&gt;
public B(UsefulInterface useful) {&lt;br&gt;
}&lt;br&gt;
&lt;/pre&gt;&lt;br&gt;
&lt;br&gt;
One caveat - if you create the B&apos;s in A&apos;s constructor you shouldn&apos;t call any methods on A in the constructor for B or in another thread until the constructor for A has exited, since A is not fully constructed yet.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402494</guid>
		<pubDate>Thu, 13 Oct 2005 15:58:43 -0800</pubDate>
		<dc:creator>true</dc:creator>
	</item><item>
		<title>By: sbutler</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402505</link>	
		<description>Also, there&apos;s a &lt;a href=&quot;http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html&quot;&gt;Java Tutorial page&lt;/a&gt; dealing with the particulars of my example. They call it an &lt;i&gt;inner class&lt;/i&gt;, but in a slightly different sense than  orthogonality&apos;s links.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402505</guid>
		<pubDate>Thu, 13 Oct 2005 16:09:10 -0800</pubDate>
		<dc:creator>sbutler</dc:creator>
	</item><item>
		<title>By: knave</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402507</link>	
		<description>It&apos;s already been stated, but java passes references to objects, not objects.  So there is no implicit copying going on.  The only time this isn&apos;t true is for native types, like int and boolean.  Those are copied by value.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402507</guid>
		<pubDate>Thu, 13 Oct 2005 16:20:00 -0800</pubDate>
		<dc:creator>knave</dc:creator>
	</item><item>
		<title>By: martinX&apos;s bellbottoms</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402516</link>	
		<description>&lt;em&gt;public class A {&lt;br&gt;
private B b = new B(this);&lt;br&gt;
private B bb = new B(this);&lt;br&gt;
}&lt;/em&gt;&lt;br&gt;
I tried that, and it didn&apos;t work for the graphics stuff I was using. (it didn&apos;t let me use b to tell a to repaint().)&lt;br&gt;
I messed around a little bit making B an inner class of A but it didn&apos;t work out so well either because there are other classes that extend B and those classes started having problems. &lt;br&gt;
&lt;br&gt;
true: i haven&apos;t tried your suggestion yet, but will it work with the paint method in A? which as I&apos;ve said, is what I really need. &lt;br&gt;
&lt;br&gt;
As you can probably tell, I&apos;m relatively new to java, and I seem to have taken on a project that&apos;s a bit out of my reach, so even if I can&apos;t apply this advice now, I&apos;ll probably remember it for later.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402516</guid>
		<pubDate>Thu, 13 Oct 2005 17:13:11 -0800</pubDate>
		<dc:creator>martinX&apos;s bellbottoms</dc:creator>
	</item><item>
		<title>By: martinX&apos;s bellbottoms</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402528</link>	
		<description>&lt;em&gt;public class A {&lt;br&gt;
private B b = new B(this);&lt;br&gt;
private B bb = new B(this);&lt;br&gt;
}&lt;/em&gt;&lt;br&gt;
on second thought... I wrote a simpler program and tried that again, and it seems to work. That&apos;s strange, because I swear I tried that and versions of that in my program and test programs several times before I even posted this question and it never worked, or even compiled, which was what led me to my apparently misguided view of references in Java. &lt;br&gt;
I&apos;ll try it again in the other program.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402528</guid>
		<pubDate>Thu, 13 Oct 2005 17:38:14 -0800</pubDate>
		<dc:creator>martinX&apos;s bellbottoms</dc:creator>
	</item><item>
		<title>By: Arthur Dent</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402706</link>	
		<description>Aah, graphics.&lt;br&gt;
I&apos;m assuming you&apos;re using swing? You need to call the repaint method in the GUI thread. Look at SwingUtilities.invokeLater and SwingUtilities.invokeAndWait ...&lt;br&gt;
&lt;br&gt;
Also, remember that the GUI subsystem is free to post your repaint request on a very low priority thread....</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402706</guid>
		<pubDate>Thu, 13 Oct 2005 21:53:54 -0800</pubDate>
		<dc:creator>Arthur Dent</dc:creator>
	</item><item>
		<title>By: eurasian</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402898</link>	
		<description>Ahhh, AWT and Swing, some of the funnest Java bugs to be found are in those inferna.. uhm.. USEFUL packages. And fwiw, Java is never typed in all caps, it&apos;s not an acronym :) (unlike the hojillion Java acronyms out there JNDI, JSP, J2EE, JMS, CMP, etc etc etc).</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402898</guid>
		<pubDate>Fri, 14 Oct 2005 07:11:59 -0800</pubDate>
		<dc:creator>eurasian</dc:creator>
	</item><item>
		<title>By: andrew cooke</title>
		<link>http://ask.metafilter.com/25482/Java-pointers#402965</link>	
		<description>you might also look at &lt;em&gt;anonymous&lt;/em&gt; inner classes.  many uses of closures (if that is what you&apos;re doing) tend to be handled by inline anonymous classes in java (typically implementing an interface of extensing an abtsract base class).  i believe (although i don&apos;t do much client gui work) this is particularly true of swing, where you&apos;re often providing callbacks and the like (&lt;a href=&quot;http://home.cogeco.ca/~ve3ll/jatutora.htm&quot;&gt;example&lt;/a&gt; - search for &quot;anonymous&quot; in that page).  you say that B is use elsewhere, but maybe you can rewrite B as an abstract class that is extended locally with the information you want to place in the closure.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2005:site.25482-402965</guid>
		<pubDate>Fri, 14 Oct 2005 08:22:03 -0800</pubDate>
		<dc:creator>andrew cooke</dc:creator>
	</item>
	</channel>
</rss>
