Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 27th, 2011, 05:25 AM
Anerxomounos Anerxomounos is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 4 Anerxomounos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 8 m 36 sec
Reputation Power: 0
Homework - 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.

Reply With Quote
  #2  
Old May 27th, 2011, 07:50 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
Do you use static variables in the class?

Reply With Quote
  #3  
Old May 27th, 2011, 08:35 AM
Anerxomounos Anerxomounos is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 4 Anerxomounos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 8 m 36 sec
Reputation Power: 0
Quote:
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!

Reply With Quote
  #4  
Old May 27th, 2011, 08:55 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
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 09:32 AM.

Reply With Quote
  #5  
Old May 27th, 2011, 08:58 AM
Anerxomounos Anerxomounos is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 4 Anerxomounos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 8 m 36 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #6  
Old May 27th, 2011, 09:10 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
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.

Quote:
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.

Reply With Quote
  #7  
Old May 27th, 2011, 09:16 AM
Anerxomounos Anerxomounos is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 4 Anerxomounos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 8 m 36 sec
Reputation 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

Reply With Quote
  #8  
Old May 31st, 2011, 02:40 PM
tfecw tfecw is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Nov 2004
Location: Washington DC
Posts: 2,753 tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 3 Days 8 h 7 m 30 sec
Reputation Power: 1570
Send a message via AIM to tfecw
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Object data overlapping.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap