Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Array of Objects

Reply
Thread Tools

Array of Objects

 
 
Sitaram
Guest
Posts: n/a
 
      02-16-2004
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[i]=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
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      02-16-2004
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.

 
Reply With Quote
 
 
 
 
Lee Weiner
Guest
Posts: n/a
 
      02-16-2004
In article <(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed) (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[i]=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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Array objects get changed when sorting the array Roedy Green Java 1 06-25-2009 08:25 PM
Re: Array objects get changed when sorting the array markspace Java 1 06-25-2009 06:22 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
pickling the objects returned by array.array() John Machin Python 2 09-03-2005 08:17 PM
Array of objects as array of POD rajkumar@hotmail.com C++ 2 03-29-2005 02:30 PM



Advertisments