The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Homework - Object data overlapping.
Discuss Object data overlapping. in the Java Help forum on Dev Shed. Object data overlapping. Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 27th, 2011, 05:25 AM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 4
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.
|

May 27th, 2011, 07:50 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Do you use static variables in the class?
|

May 27th, 2011, 08:35 AM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 4
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!
|

May 27th, 2011, 08:55 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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.
|

May 27th, 2011, 08:58 AM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 4
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
|

May 27th, 2011, 09:10 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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.
|

May 27th, 2011, 09:16 AM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 4
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 
|

May 31st, 2011, 02:40 PM
|
|
Contributing User
|
|
Join Date: Nov 2004
Location: Washington DC
|
|
|
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|