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. Advertising

  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. Advertising

  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. 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. Replies:
    2
    Views:
    394
    Victor Bazarov
    Mar 29, 2005
  2. John Machin
    Replies:
    2
    Views:
    302
    Reinhold Birkenfeld
    Sep 3, 2005
  3. 7stud
    Replies:
    11
    Views:
    750
    Dennis Lee Bieber
    Mar 20, 2007
  4. markspace
    Replies:
    1
    Views:
    409
    markspace
    Jun 25, 2009
  5. Roedy Green
    Replies:
    1
    Views:
    454
    Roedy Green
    Jun 25, 2009
Loading...

Share This Page