Array of Objects

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
 
S

Sudsy

Sitaram wrote:
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.
 
L

Lee Weiner

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top