You WILL get spanked for that name when the Sherrifs come around!
To call a normal public method defined in another class you need to have an object instance of that class.
I omitted the full package path to the class because I prefer to import the class. Lemme know if you haven't gotten around to "import" yet. What I did there was declare a variable of type Vauxhall and create an object instance of Vauxhall. Then I call a method on the new object. The method probably looks like:
You can in fact call methods on the class as well as on an object instance of the class. These are special methods, often called "class methods" just because we want to call them on the class. They are identified by the keyword "static", eg "public static int getMake()" Because they belong to the class and not a particular object, they work with information that's common to all Vauxhalls in the world, and not information that's relative to your Vauxhall or mine. A method like getColor() would not be a good static class method ... unless all Vauxhalls were the same color.
Does that help?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
posted 14 years ago
Thanks for the reply but i've got a few more questions im afraid.
I understand the import concept i've been using it in my code to create objects from classes defined in other files. However..
What i'm trying to do here is kinda complex to explain and I doubt it's even possible, most likely i've become confused somewhere, anyways ill try explain.
I have a class called World (its not really called this) and World creates an instance of Bank and this Bank has an ArrayList which holds instances of another class called Account. Now I want to use a method (createAccount) within the Bank class, but I want to call this method from a class called FileExtractor.
If I was in World I would simpy do this..
However i'm not, i'm reading a file and creating new Accounts based upon the information contained in the file. So I need to use the above function but I need to call it from FileExtractor.
Is this all possible? Or have I written some awful code? Thanks
Originally posted by N/A N/A: Is this all possible? Or have I written some awful code?
Looks like your heading down that way...
Try to keep your classes singularly focused on one purpose. FileExtractor should be focused on extracting things from a file. It shouldn't know anything about an Account. The Bank should probably know about Accounts. It would be more appropriate for the Bank to ask the FileExtractor for information. The Bank would then take the information provided by FileExtractor and create new Accounts.
Now for the spanking that Stan was talking about (taking out paddle) (*smack*): "N/A N/A" does not comply with the JavaRanch Naming Policy. Please edit your profile and change your publicly displayed name to something that does. Thanks! (putting paddle away).
Edit: (taking paddle out again)
"Gonzo" doesn't cut it either, pardner. Please read the policy again.
Thanks for the quick reply. I think the problem is im using JUnit and adopting a TDD approach, this means im creating my "larger" objects in the tests class when I shouldn't.
I'll try moving them and then get back to ya. Thank you
posted 14 years ago
I think i've got my code working thanks to your suggestions I appreciate that.
I have another one though. Basically I'm reading a file and trying to instantiate objects depending on the values of each line in the file.
Note: A line of the file looks like this - AccountNo Balance ClientName
My FileHandler class reads the lines but I need to return the data found on each line to the Bank class which will create the Accounts. So as I see it I have two options.. either call the method which reads a line repeatedly from the Bank class until I have read the whole file.
Read the file and create a hashtable (I think this would be the right collection) which would store the values read from the file.. e.g. account number and balance, then pass this hashtable to the Accounts class which would then create the accounts by reading the Hashtable.
Which of the two would be the most efficent solution? Or is there a better way of doing this? I'm not sure if my understanding of a hashtable is correct? Is it like a spreadsheet/database etc.. where it consists of a number of columns each of which are related (when on the same row)?
David, is this work-related code or school-related? I'm asking because if it's work related, is there are reason for using a text file instead of a proper database? Can you use third-party code? If it's school-related, are you allowed to use third-party code or do you have to write everything yourself?
The third-party code I'm thinking about that would be useful in your case is the Jakarta Commons BeanUtils package. BeanUtils provides a way to populate a JavaBean from values taken from a Map where the map keys correspond to properties in your JavaBean. This is a flexible and reusable solution. On the other hand, if this is school-related, you might not need to be so flexible/reusable or if you did, you might have to code it yourself, in which case, the Map strategy is still a good way to go.
Practice only makes habit, only perfect practice makes perfect.—every music teacher ever
Practice mindfully by doing the right things and doing things right.— Junilu