Object data overlapping.

Discussion in 'Java' started by Anerxomounos, May 27, 2011.

  1. Anerxomounos

    Anerxomounos New Member

    Joined:
    May 27, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have this code that i'm using to test if my program works, but all the Account type objects are somehow getting the same data inserted.
    Code:
    Account acc1 = new Account();
           Account acc2 = new Account();
           Account acc3 = new Account();
           acc1.insert("AAA", 1, 2, 3, 4, 5, 6);
           acc2.setCode("CCC");
           acc3.setCode("BBB");
    It prints out : BBB 1 2 3 4 5.0 6.0
    for all 3 of them, while it should print:
    AAA 1 2 3 4 5.0 6.0
    CCC 0 0 0 0 0.0 0.0
    BBB 0 0 0 0 0.0 0.0

    The Account class works perfectly, and I know this because it got 10/10 on my previous project.
     
  2. Avenger625

    Avenger625 New Member

    Joined:
    Feb 1, 2011
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    Post the codes for insert() and setcode().
    It is not possible to tell what was wrong from what u have posted.
    Post some more parts of the code!!!
     
  3. ewaldhorn

    ewaldhorn New Member

    Joined:
    Feb 16, 2010
    Messages:
    36
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    Cape Town, South Africa
    Home Page:
    http://www.javak.co.za
    Hi.

    Since I haven't seen your source code, it's a bit difficult to figure out what's going on. You seem to be a new Java programmer, so perhaps your class is working, but it contains static variables?

    See, static variables are shared by all instances of a class, which would explain why altering one class has an effect on the rest as well. Remove any static keywords you have in the declaration of your variables and see if that helps.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice