wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Allowing Class A to view Class B's values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Allowing Class A to view Class B Watch "Allowing Class A to view Class B New topic
Author

Allowing Class A to view Class B's values

David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
I have two classes (A+B) I want Class B to be able to call functions on an object instantiated in Class A.

I created the object in class A using the following code:

public org.simulator.Car Vauxhall


Now I presumed i'd be able to use the following code in Class B to call a function on Vauxhall


simulator.Vauxhall.WhateverFunction()


Am I not right in thinking this? I set the access value of the object to Public in the hope this would work. If not how do people get around this?

Thanks so much this is really important! Thanks
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
Hi Stan,

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
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4792
    
    8

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.

(*smack*)
[ November 11, 2004: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
Hi,

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
David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
Hey,

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.

OR

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)?

Thanks
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4792
    
    8

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.
David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
Hi, its uni related i'm afraid. I'm very restricted as to what I can and can't use. Third Party code is definitely out and yes I do have to code things myself.

Is a hashtable definitely not the way to go? I've been trying to find explanations with diagrams but haven't found much yet.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Allowing Class A to view Class B's values