Do not declare your variable as static. Is there a reason why you are
doing that. If you remove static from your Course class, it will work
public class Course implements Serializable {
private String courseName;
/**
* @return
*/
public String getCourseName() {
return courseName;
}
/**
* @param string
*/
public void setCourseName(String string) {
courseName = string;
}
}
vsingr - 12 Dec 2005 19:02 GMT
Well I had my own reasons for declaring as static. I guess i will have
to work around that now.
thanks
Rebecca - 12 Dec 2005 20:45 GMT
You may want to look into when it makes sense to declare something
static - here's a descent article.
http://www.javaworld.com/javaworld/javaqa/2001-11/03-qa-1121-mrhappy.html
vsingr wrote On 12/12/05 13:40,:
> Eric
> Here it goes
[quoted text clipped - 24 lines]
> }
> }
That much is fine: You create ten distinct Course
objects, give them names, put them in the ArrayList,
pull them out again in the form of an array, and print
the names. All is well, so far.
> There is also an Object called course which is defined below
>
> public class Course implements Serializable {
>
> private static String courseName;
Aha! All the Course instances share a single
courseName variable. When you retrieve the ten
different Course objects from the array and call
their getCourseName() methods, they all return that
same shared courseName. Ten Courses, only one name.
Without "static," each Course would have its own name,
distinct from the names of the other Courses.

Signature
Eric.Sosman@sun.com
ricky.clarkson@gmail.com - 12 Dec 2005 20:22 GMT
As a general rule, I would avoid non-final static fields, as they are
often used as crude implementations of the Singleton [anti]pattern, aka
global variable. If you made the above field final you would get
compile errors, and have to look at it a bit closer.
All rules have exceptions, but surely there must be an exception to
that rule.