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


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

Need advice on Class Member instantiation

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!feeder2.cambriumusenet.nl!feed.tweaknews.nl!209.197.12.242.MISMATCH!nx01.iad01.newshosting.com!newshosting.com!69.16.185.11.MISMATCH!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!spln!extra.newsguy.com!newsp.newsguy.com!drn
From Roger T <Roger_member@newsguy.com>
Newsgroups comp.lang.java.help
Subject Need advice on Class Member instantiation
Date 22 Sep 2011 19:55:16 -0700
Organization NewsGuy - Unlimited Usenet $19.95
Lines 35
Message-ID <j5gsek07b6@drn.newsguy.com> (permalink)
NNTP-Posting-Host p3f453f652f575033986a3a3030be12d85aa7765530e5de8e.newsdawg.com
User-Agent Direct Read News 5.60
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.help:1101

Show key headers only | View raw


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;
 int age;

 public static void main(String arguments[]) {

   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);
   System.out.println("Family surname is " + FamilyMember.surname);

   System.out.println("Sis gets married...");

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

  }
}

Back to comp.lang.java.help | Previous | NextNext 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