Results 1 to 3 of 3
  1. #1
    JavaClueless is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Unhappy Problem with Vector of Objects

    Hello,

    I am new to both this forum and java and have a question about a problem with my code. I have written a class (say, Test_obj), and now I am trying to make a Vector of objects from that class. However, as I add more objects to the vector, it adds the object, but also replaces all of the previously added objects by it. That is, given this code:

    public class Test {
    private static Vector<Test_obj> tests = new Vector<Test_obj>();

    public static void main(String[] args) {
    for(int i=0; i<3; i++) {
    String new_str = "blah"+i;
    Test_obj new_obj = new Test_obj(new_str, new_str);
    tests.add(new_obj);
    }
    for(int i=0; i<3; i++)
    System.out.println(tests.elementAt(i).get_word());
    }
    }
    The print out is:
    blah2
    blah2
    blah2
    instead of desired:
    blah0
    blah1
    blah2

    Please, help. What am I doing wrong? How can I have a vector of different Test_obj in it? This is not the same, if I use String instead of Test_obj, which is class I have written myself. Here is the code for Test_obj:

    public class Test_obj {
    private static String word = new String();
    private static String second_word = new String();

    public Test_obj(String one, String two) {
    word = one; second_word = two;
    }
    public Object get_word() {
    return word;
    }
    }


    Thanks a lot!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    statics -- get rid of them!

    specifically, why did you make your class fields static, and do you understand why this is ruining your logic?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    @OP: your problem has nothing to do with Vector.

    A strong nudge towards cluefulness:

    Java Code:
    // Test.java
    public class Test {
        public static void main(String[] args) {
            Test_obj first = new Test_obj("FIRST", "FIRST");
            Test_obj second = new Test_obj("SECOND", "SECOND");
    
            System.out.println("first word is " + first.get_word());
        }
    }
    
    // Test_obj.java
    public class Test_obj {
        private static String word = new String();
        private static String second_word = new String();
    
        public Test_obj(String one, String two) {
            word = one; second_word = two;
        }
    
        public Object get_word() {
            return word;
        }
    }

    Both of Fubarable's questions are worth answering. Even "I had no reason"/"No" would be an advance on silence.

Similar Threads

  1. Vector objects
    By Dolphin in forum Java Software
    Replies: 0
    Last Post: 03-24-2009, 08:45 AM
  2. Vector objects
    By Dolphin in forum java.util
    Replies: 0
    Last Post: 03-24-2009, 08:36 AM
  3. Working with Vector objects + textfile
    By SGRocker in forum New To Java
    Replies: 5
    Last Post: 09-16-2008, 10:55 PM
  4. Retrieving out objects from Vector
    By tuckker in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 05:52 AM
  5. Vector containing heterogeneous objects
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-28-2007, 10:19 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •