Array of Objects

Discussion in 'Java' started by Sitaram, Feb 16, 2004.

  1. Sitaram

    Sitaram Guest

    Hi,
    I am trying to use an array of objects and i am getting an awkward error.
    I know i am doing something stupid somewhere but cant quite find it.

    here is my code. i am creating a class t1 and then an array of objects of t1
    in class temp.
    i am inserting values for two objects but when i display i see that it is the
    second one which has been written twice.


    class t1
    {
    public static int x;
    public static String y;

    public static void pushin(int p)
    {
    x=p;
    }
    public static void pusht(String p)
    {
    y=p;
    }
    public void disp()
    {
    System.out.println(x);
    System.out.println(y);
    }
    };
    class temp
    {
    public static void main(String args[])
    {
    t1[] t=new t1[100];
    for(int i=0;i<100;i++)
    {
    t=new t1();
    }
    t[0].pushin(1);
    t[0].pusht("help");
    t[1].pushin(2);

    t[1].pusht("me");
    for(int j=0;j<2;j++)
    {
    t[j].disp();
    }
    }
    }
    I am getting 2 me 2 me as the output.
    Can you please tell me where I am making the mistake?

    THanks,
    Sorin
     
    Sitaram, Feb 16, 2004
    #1
    1. Advertisements

  2. Sitaram

    Sudsy Guest

    Sitaram wrote:
    <snip>
    > class t1
    > {
    > public static int x;

    public int x;
    > public static String y;

    public String y;
    >
    > public static void pushin(int p)

    public void pushin(int p)
    > {
    > x=p;
    > }
    > public static void pusht(String p)

    public void pusht(String p)
    > {
    > y=p;
    > }
    > public void disp()
    > {
    > System.out.println(x);
    > System.out.println(y);
    > }
    > };

    <snip>

    By making the methods and variables static you're saying that
    they have class scope rather than instance scope. Make the
    changes listed above and the results will be what you expect.
     
    Sudsy, Feb 16, 2004
    #2
    1. Advertisements

  3. Sitaram

    Lee Weiner Guest

    In article <>, (Sitaram) wrote:
    >Hi,
    >I am trying to use an array of objects and i am getting an awkward error.
    >I know i am doing something stupid somewhere but cant quite find it.
    >
    >here is my code. i am creating a class t1 and then an array of objects of t1
    >in class temp.
    >i am inserting values for two objects but when i display i see that it is the
    >second one which has been written twice.
    >
    >
    > class t1
    >{
    > public static int x;
    > public static String y;
    >
    >public static void pushin(int p)
    > {
    > x=p;
    > }
    >public static void pusht(String p)
    > {
    > y=p;
    > }
    >public void disp()
    > {
    > System.out.println(x);
    > System.out.println(y);
    > }
    >};
    >class temp
    >{
    > public static void main(String args[])
    > {
    > t1[] t=new t1[100];
    > for(int i=0;i<100;i++)
    > {
    > t=new t1();
    > }
    > t[0].pushin(1);
    > t[0].pusht("help");
    > t[1].pushin(2);
    >
    > t[1].pusht("me");
    > for(int j=0;j<2;j++)
    > {
    > t[j].disp();
    > }
    >}
    >}
    >I am getting 2 me 2 me as the output.
    >Can you please tell me where I am making the mistake?


    The mistake is that you're declaring the two variables, x and y, as static,
    which means that all objects of the class t1 share the same variables. Remove
    the static qualifier from the variables and from the two pushXX methods.

    Lee Weiner
    lee AT leeweiner DOT org
     
    Lee Weiner, Feb 16, 2004
    #3
    1. Advertisements

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. Replies:
    2
    Views:
    486
    Victor Bazarov
    Mar 29, 2005
  2. John Machin
    Replies:
    2
    Views:
    368
    Reinhold Birkenfeld
    Sep 3, 2005
  3. 7stud
    Replies:
    11
    Views:
    1,052
    Dennis Lee Bieber
    Mar 20, 2007
  4. markspace
    Replies:
    1
    Views:
    508
    markspace
    Jun 25, 2009
  5. Roedy Green
    Replies:
    1
    Views:
    541
    Roedy Green
    Jun 25, 2009
  6. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    822
    David RF
    Sep 3, 2009
  7. Tom
    Replies:
    3
    Views:
    402
    salsablr
    Dec 20, 2004
  8. Tuan  Bui
    Replies:
    14
    Views:
    784
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...