Type casting

Discussion in 'Java' started by programming, Dec 16, 2006.

  1. programming

    programming Guest

    Hi all, i was just wondering with the below program what effect what
    type casting have on the final output. I tried compiling the sample
    below, but there were to many bugs in the program to get it to work. I
    think that type casting would copy the value "cufflinks for Aus" into
    T2, and it would be printed out twice. Can somebody verify this with
    me.


    public class Thingy
    {
    public TextField value;

    public Object clone()
    {
    Thingy n;

    n=new Thingy();
    n.value=this.value;

    return n;
    }
    }


    ------------------------------------------------------------------------------------------------------------------------------------------------
    public class Question
    {
    Thingy t1,t2;

    public static void main(String args[])
    {

    t1= new Thingy();

    t1.value.setText("Cufflinks for Aus");

    t2=(Thingy) t1.clone();
    t2.value.setText("Bah Humbug!");
    }


    }
     
    programming, Dec 16, 2006
    #1
    1. Advertising

  2. programming

    Daniel Pitts Guest

    programming wrote:
    > Hi all, i was just wondering with the below program what effect what
    > type casting have on the final output. I tried compiling the sample
    > below, but there were to many bugs in the program to get it to work. I
    > think that type casting would copy the value "cufflinks for Aus" into
    > T2, and it would be printed out twice. Can somebody verify this with
    > me.
    >
    >
    > public class Thingy
    > {
    > public TextField value;
    >
    > public Object clone()
    > {
    > Thingy n;
    >
    > n=new Thingy();
    > n.value=this.value;
    >
    > return n;
    > }
    > }
    >
    >
    > ------------------------------------------------------------------------------------------------------------------------------------------------
    > public class Question
    > {
    > Thingy t1,t2;
    >
    > public static void main(String args[])
    > {
    >
    > t1= new Thingy();
    >
    > t1.value.setText("Cufflinks for Aus");
    >
    > t2=(Thingy) t1.clone();
    > t2.value.setText("Bah Humbug!");
    > }
    >
    >
    > }


    Hmm, I think its time for you to get a Java book.
     
    Daniel Pitts, Dec 16, 2006
    #2
    1. Advertising

  3. programming

    Lew Guest

    > programming wrote:
    >> public class Thingy
    >> {
    >> public TextField value;


    Most times one should keep member variables private and use accessor methods
    to get at them.

    >>
    >> public Object clone()

    Your intent is to override Object.clone(), correct?

    >> {
    >> Thingy n;
    >>
    >> n=new Thingy();
    >> n.value=this.value;


    This causes the clone to share a reference to the same TextField as its
    progenitor's.

    >>
    >> return n;
    >> }
    >> }
    >>
    >>
    >> ------------------------------------------------------------------------------------------------------------------------------------------------
    >> public class Question
    >> {
    >> Thingy t1,t2;
    >>
    >> public static void main(String args[])
    >> {
    >>
    >> t1= new Thingy();
    >>
    >> t1.value.setText("Cufflinks for Aus");
    >>
    >> t2=(Thingy) t1.clone();
    >> t2.value.setText("Bah Humbug!");


    This replaces the text attribute in the value field that is pointed to by both
    t1 and t2.

    >> }
    >>
    >>
    >> }


    Not only is the TextField 'value' shared, but it has public access, meaning
    that it's subject to change any ol' which where.

    - Lew
     
    Lew, Dec 17, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,816
    Andrew McDonagh
    Jan 8, 2005
  2. heyo
    Replies:
    3
    Views:
    931
    Dan Pop
    Apr 1, 2004
  3. pete
    Replies:
    4
    Views:
    809
    Dan Pop
    Apr 2, 2004
  4. Wally Barnes
    Replies:
    3
    Views:
    533
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    576
    James Kanze
    Dec 20, 2009
Loading...

Share This Page