#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Object data overlapping.


    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. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,719
    Rep Power
    348
    Do you use static variables in the class?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    Do you use static variables in the class?
    Yes, I do. And please don't tell me that's the problem because I'm gonna murder my java professor!
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,719
    Rep Power
    348
    With static variables, there is only ONE variable that is shared between all the instances of the class objects. In other words, all the instances of the Account class will have the same value for all variables that are defined as static.
    Last edited by NormR; May 27th, 2011 at 10:32 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    With static variables, there is only ONE variable that is shared between all the instances of the class objects. In other words, all the instances of the Account class will have the save value for all variables that are defined as static.
    Oh my god, I think I have a lot of typing to do...
    However, how would I use non-static variables for static methods? (Because my dear prof won't explain anything about static and non-static but still force me to use static in front of my methods. Also my book is crap and provides no guidelines at all regarding this subject.)


    For example I need to have this method in my Account class:
    Code:
    public static String values()
        {
            return code + "\t" + accountNo + "\t" + day + "\t" + month + "\t" + year + "\t" + balance + "\t" + interestRate + "\n";
        }
    It needs to be static because it's called by another static method ( a main() in some other class). And it tells me my variables should also be static for it to work. :S
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,719
    Rep Power
    348
    If the values method is static, it can only see static values.
    If you pass a reference to an object as an arg to the values method, then it could get the contents of variables from that object.

    It needs to be static because it's called by another static method ( a main() in some other class)
    You should redesign your code so you don't need to use static.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    4
    Rep Power
    0
    Actually dear friend, don't even bother explaining. It is not your job to teach me, it my professor's.

    So I'm getting rid of all the statics and if I don't get a perfect score for that reason, well he can kiss my ***. He can't expect me to know details he hasn't taught me, and I know it might be trivial for many programmers, but I just started java 3months ago and I have read 10 times more information than that included in my pathetic book.

    Same thing happened with my other project, where he wanted me to build some calendar without using the Calendar classes. However, he didn't even mention I shouldn't use them, neither did he mention what I should use. Therefor, my project will get some seriously dissapointing grade, despite my countless hours of work on it.

    Thanks again for pointing out that static thing, I will surely remember it from now, because I always remember things that infuriate me
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2004
    Location
    Washington DC
    Posts
    2,756
    Rep Power
    1572
    Colleges are a lot of things, but they are most certainly not about spoon feeding information. No one cares about what your prof did or didn't teach you. You have all the tools and are an adult. Make it happen.
    Open for extension, closed for modification

IMN logo majestic logo threadwatch logo seochat tools logo