-
Accessing Object Variables of an Object, stored in a Vector
Hi,
How do I access the object variables of an object which is stored in a vector?
e.g.
public class students
{
pubilc String Name;
public students(String aName)
{
this.Name = aName;
}
}
// somewhere in the main method
Vector allStudents = new Vector();
aStudent = new Students("Sanjay");
allStudents.add(aStudent);
for (int k=0; k < allStudents.size(); k++)
{
Object m = allStudents.get(k);
System.out.println(m.Name);
}
This is throwing up an error "cannot resolve symbol variable Name".
Can anyone see where I am going wrong?
Thanks in advance,
Sanjay.
-
System.out.println(((Students)m).Name);
with Java 5.0 it can be done different. I've not moved to it so I don't know the syntax off the top off my head.
-
That is is the correct solution!
Thanks.
Sanjay.
-
Well sort of. It works for what you are doing. If you want to access more than one attribute or method, go ahead and cast it on the get from the Vector.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks