S
Sitaram
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
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