Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #1106
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: Need advice on Class Member instantiation |
| Date | 2011-09-22 23:34 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <j5gupo$jfp$1@dont-email.me> (permalink) |
| References | <j5gsek07b6@drn.newsguy.com> |
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 | Next — Previous in thread | Next in thread | Find similar
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