File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes accessing the setters/getters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "accessing the setters/getters" Watch "accessing the setters/getters" New topic
Author

accessing the setters/getters

Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
So the code in the action performed would be:
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Mar 17, 2003
Posts: 123
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Mar 17, 2003
Posts: 123
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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!
 
jQuery in Action, 2nd edition
 
subject: accessing the setters/getters
 
Similar Threads
java anotation
iBatis Mapping Help
NX: exposing the MetaData to the client
need help with understanding instructions
using the getters