Results 1 to 4 of 4
  1. #1
    Anerxomounos is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default Object data overlapping.- Need help with static variables.

    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.

    Java 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.

    Also, I understand that the problem is probably caused by the use of static variables in the Account class. How do I fix this? Mind you, I can't really take off the static infront of those variables because the methods that use them are static too. Also, if I use static in front of my acc1,acc2,acc3 I just get a java.lang.NullPointerException.

    Please help me :D

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,767
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Anerxomounos View Post
    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.

    Java 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.

    Also, I understand that the problem is probably caused by the use of static variables in the Account class. How do I fix this? Mind you, I can't really take off the static infront of those variables because the methods that use them are static too. Also, if I use static in front of my acc1,acc2,acc3 I just get a java.lang.NullPointerException.
    If the fields where you store "AAA", 1, 2, 3, 4, 5 and 6 are static, those fields belong to the class and therefore, effectively belong to all instantiations of the class. Make those fields non-static and also (as you already mentioned) make the methods that manipulate the fields non-static. I think 10/10 is a bit overrated ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Quote Originally Posted by Anerxomounos View Post
    The Account class works perfectly, and I know this because it got 10/10 on my previous project.
    Five points for compiling, and five for producing the correct output? Your instructor probably didn't even look at your code.

    Post it here and let's have a look. :D
    Get in the habit of using standard Java naming conventions!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

Similar Threads

  1. Replies: 6
    Last Post: 02-15-2010, 05:31 AM
  2. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 06:48 PM
  3. static are instance variables
    By gabri in forum Advanced Java
    Replies: 12
    Last Post: 09-30-2008, 07:30 PM
  4. Using Static Variables
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:08 AM
  5. Help with static variables
    By bbq in forum Advanced Java
    Replies: 1
    Last Post: 06-28-2007, 06:38 PM

Posting Permissions

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