Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing the setters/getters

 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where would I access the setters and getters? Would that be in the button class where I get the data now? Do I have them set up right?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This post makes more sense in the context of your other thread on this topic.
After looking at that thread, I can answer your question in context: you would use the setters in the actionPerformed method of the ButtonListener class inside the AccountFrame class. You would use them to set the member variables of the transaction object to hold the values from the text fields in your dialog.
In the program you're supposed to be writing, you'd never actually use the getters. But you can sure imagine a complementary program that reads the object back out of the file; that program would need the getter methods.
One more thing: in your example code, you're showing the getters and setters as private methods -- this would make them inaccessible to other classes, which would make them not very useful, right? You want them to be public:

[ September 26, 2003: Message edited by: Ernest Friedman-Hill ]
 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the code in the action performed would be:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,
although
I'll take the opportunity to point out that comparing strings with "==" is running with scissors. All the identical string literals in a program will compare "true" using "==", but if you read an identical String from a file (for example) it will compare "false" using "==" but true using the equals() method.
The "==" operator asks "are these two things physicsally the same object (same memory address)?" while the equals() method asks "Are these effectively equivalent, even though they may be separate objects?" All the string literals "Apply" in a given program are represented by the same physical String object, while an "Apply" read out of a file will be a new, independent object. So your code will work for now, but it's super fragile, and generally bad form.

Therefore, I'd correct your code to read

if (e.getActionCommand().equals("Accept")) {
gan = txtGrpAcctNumber.getText();
gan.setGrpAcctNumber(gan);
}

Finally -- the dialog class doesn't need member variables to hold things like "gan" anymore; they get shuttled directly from the textfield to the transaction. So the current definition of "gan" would go away, and this code might change further to:

if (e.getActionCommand().equals("Accept")) {
String gan = txtGrpAcctNumber.getText();
gan.setGrpAcctNumber(gan);
}
 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing, then I might have it. I get a compile error:
C:\Candy\Java\Reimbursement.java:135: cannot resolve symbol
symbol : method setGrpAcctNumber (java.lang.String)
location: class java.lang.String
gan.setGrpAcctNumber(gan);
^
This is in the action performed. Above that I have added the method:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, I was just copying and modifying your code, and didn't notice this. You're calling the setter on "gan" which is a String; you mean to be calling it on the transaction object (don't recall what that class is called; I mean the instance of the class with the getters and setters.)
 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried Reimbursement, AccountFrame, AccountTransaction. I don't know what should be in the parenthesis with gan.

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole point of this app is that when the button is pressed, you create an AccountTransaction object, populate all its fields using the setters, and then serialize it to a file (if memory serves.) So in this handler, you want to create an AccountTransaction object, and start calling its setters. Right?

A hint: start putting these classes into separate files. AccountTransaction, in particular, should be moved into a file AccountTransaction.java . Among other benefits, this makes it easier to see which parts of your code will compile and which parts won't!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic