Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1106

Re: Need advice on Class Member instantiation

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.java.help
Subject Re: Need advice on Class Member instantiation
Date Thu, 22 Sep 2011 23:34:46 -0400
Organization A noiseless patient Spider
Lines 56
Message-ID <j5gupo$jfp$1@dont-email.me> (permalink)
References <j5gsek07b6@drn.newsguy.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Fri, 23 Sep 2011 03:35:20 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="f8igmItKsWs6nM5YanFxAA"; logging-data="19961"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+b6HbO6tT2EkqriVtZuJRI"
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20110902 Thunderbird/6.0.2
In-Reply-To <j5gsek07b6@drn.newsguy.com>
Cancel-Lock sha1:D3LdAsXCOXq2qIyPUpzrLBxXfek=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.help:1106

Show key headers only | View raw


On 9/22/2011 10:55 PM, Roger T wrote:
> Can someone give me some guidance as to what I am missing here?
> When I run the program, ALL of the FamilyMembers (including the class
> FamilyMember) get their surname changed to Goober
> even though only Sis explicitly gets changed.
> It seems as if they are both pointing to the same instance
> (similar to C pointer), but isn't this code creating dad and sis as
> two separate instances?
>
> Thanks for advice.
> // CODE STARTS
>
> class FamilyMember {
>   static String surname = "Carrington";
>   String name;

     `static' means "This field belongs to the class as a whole, not
to any individual instance.  There is exactly one `surname', no
matter how many `FamilyMember' instances you create: one, two, two
thousand, or even zero."

     That is, "It seems as if they are both pointing to the same
instance" is exactly right: There is one and only one `surname',
shared by all `FamilyMember' instances and by the `FamilyMember'
class as a whole.

>     FamilyMember dad = new FamilyMember();
>     FamilyMember sis = new FamilyMember();
>
>     System.out.println("Dad's surname is " + dad.surname);
>     System.out.println("Sis's surname is " + sis.surname);

     Some people think the designers of the Java language erred in
allowing `dad.surname' and `sis.surname', because it looks like
you're mentioning instance-specific fields rather than class-wide
fields.  The argument has some merit, but it's rather pointless:
Java is as it is, and that's the way things are.  The more usual
way to refer to a class field (the way the second-guessers would
prefer were the only way) is to use the class name rather than a
reference to any particular instance: `FamilyMember.surname'.

     Here's something to try in an idle moment:

	FamilyMember evilTwin = null;  // estranged non-person
	System.out.println("Twin's surname is " + evilTwin.surname);

Question: Why does this work, even though the family has disowned
the even twin and never mentions him any more?  Contrast with

	System.out.println("Twin's first name is " + evilTwin.name);

... and explain why the two behave so differently.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Need advice on Class Member instantiation Roger T <Roger_member@newsguy.com> - 2011-09-22 19:55 -0700
  Re: Need advice on Class Member instantiation Roedy Green <see_website@mindprod.com.invalid> - 2011-09-22 20:28 -0700
  Re: Need advice on Class Member instantiation Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-22 23:34 -0400
  Re: Need advice on Class Member instantiation Bjørn Steensrud <bjornst@skogkatt.homelinux.org> - 2011-09-30 15:06 +0200

csiph-web