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 Refering to another class's instance of a different class, inside a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Refering to another class Watch "Refering to another class New topic
Author

Refering to another class's instance of a different class, inside a class

Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

I have a program that uses three .class files.
Two of the classes (class 1 and 2) have an instance of the third class (class 3).
My question is, can class 1 refer to class 2's instance of class 3, and if so, then how?
Class one can extend class 3.

Awaiting,
Eden
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Sure:
Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

My bad. I didn't mean referring. I meant changing.
Say, a private Boolean type and get and set methods for it in class 3.
Class 1 and 2 have an instance of class 3.

Class1:


Class2:


Class3:


Hope it's clearer now.
Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

If someone would please point out where my mistakes are in my example and fill in the if statement it would be much appreciated. I know the example Stephan gave me should explain it, but I don't quite get it.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Eden Landau wrote:Hope it's clearer now.

Not really. If all the classes are in the same package, then they will all have access to anything they like in any other class, because nothing is private.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Your getBoolean() method has to be called with ().
If it returns a boolean and not a Boolean, its return value cannot be null and you cannot use the != null test on it. You can however simply writeif (xxx.getBoolean()) something(); else somethingDifferent();
How did you think up a method name like myPoint()?
Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

Campbell, small stuff, don't matter one bit, and 'cause it's my point.
Winston, I just don't know how.

Think of it like this. If class 3 couldn't have an Class1 instant of it's own, but have an instance of Class2. How would it call method class2.class1.getBoolean() (or something)?

Edit: The above won't work, by the way, because it has to access Class2's instance directly, and not an instance of Class2.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
I think the problem is you are misunderstanding what it means to "have" an instance. Instances of Class1 and Class3 are created in the main method of Class2, but that does NOT mean that Class2 "has" them. Class2 would "have" an instance of Class1 if, anywhere within Class2, the instance of Class1 could be reached.
Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

I'm not seeing a difference.
If I create instances of class, the class I created them in would "have" the instances of the classes, no?
And I'm not seeing how this answers the question at hand.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Eden Landau wrote:Campbell, small stuff, don't matter one bit, and 'cause it's my point.
. . .
You try to get the code you posted to compile, then.
Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

The code I posted is just an example! >.>
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Eden Landau wrote:I'm not seeing a difference.
If I create instances of class, the class I created them in would "have" the instances of the classes, no?

No. As Dennis already said.

And I'm not seeing how this answers the question at hand.

What question? We've yet to get an example that actually means anything yet.

My suggestion would be to have a look at the Java tutorials section on "visibility", and see if it makes sense. If not, copy an example from there and ask again, because I strongly suspect that you're missing the keyword 'private' from your examples above; and without the correct code, it's very difficult to answer your query.

Winston
Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

Dammit. You asked for it, internet!
Here's my code:

Class Methods




Class DiceRoll:




Class Console (named so for no reason whatsoever):


Now I feel all stupid for reverting to actually have to post my 350 lines of code.
What I need is for Console.processDev() to call Methods.setChange(), in a way that will affect DiceRoll if statements (when calling methods.setSideNum() and methods.setDiceNum()), assumably through DiceRoll's instance of Methods.

There!

Edit: Class Console extends class Methods as an experiment to try and do that thing I've been trying to do. I really can't explain any of this any better.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Eden Landau wrote:Dammit. You asked for it, internet!
Here's my code:
...
Now I feel all stupid for reverting to actually have to post my 350 lines of code.
What I need is for Console.processDev() to call Methods.setChange(), in a way that will affect DiceRoll if statements (when calling methods.setSideNum() and methods.setDiceNum()), assumably through DiceRoll's instance of Methods.

There!

Edit: Class Console extends class Methods as an experiment to try and do that thing I've been trying to do. I really can't explain any of this any better.

Much better. Now we have a proper question, and the code to back it up.

OK, my first glance says to me that you're thinking about this far too procedurally. You've clearly analysed how you want to do this a lot; but you're now hamstrung by your design because you should have started out by first asking yourself what you want to do.

I can tell you for starters that your Methods class is a huge ball and chain; and you're in for a world of hurt if you keep going down this road.
Method-based classes like that are rare in Java, and usually written to stand alone (ie, they are final) for holding utility methods.

Forgetting Java for a moment: What do a Dice Roll and a Console have in common?
Answer: About the same amount as a Dice Roll and a Venezuelan Back-Scratcher ... ie, nothing.
And yet you have them both extending from the same class.

The fact that they have some tasks that might look the same is a total red herring.

Getting back to the "what you want to do": Plainly you want to write a game that involves a user rolling a dice (or, more properly, a die) that can be of any number of sides.
I'm still not quite sure what the object of the game is, but just by extracting the "things" from the above sentence, I can see that you'll probably need a Game class (although you might want to give it a proper name, like DiceRoller), a Dice class, and (possibly) a User class.

What do you need to do with a Dice? Well, it sure looks like you're going to need to roll() it, so that's a likely method; and if you want to be able to roll it several times, you might want that method to take a number as a parameter. When you set up a Dice, you need to know how many sides it has, so I suspect that should probably be passed to its constructor, and maybe stored in the Dice as an attribute.

Do you see what's happening? Rather than charging straight through to the "how am I going to do this?" (the methods), I'm dealing with "what do I need to do this?" (the classes). Not a single line of code written, and I've already got three classes and a likely method.

I have to pop out now, but I'll be back later. Have a think about it.
I hate to say, but I suspect you're going to have to start again, pretty much from scratch. Keep the old classes around though; you might be able to copy some code over from them later on.

BTW, knowing something about what the objective of the game is would also be useful.

Winston
Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

Please help me with this.
Start reading for class DiceRoll and read other non-obvious methods as needed.
Didn't see that ^.

It's not a game, actually, but a dice rolling utility, though I see your point.
Any more specific structure tips? How should I split the methods, if at all?
To be more specific, what other way is of programming? I know it sounds silly, and I know there ARE other ways, but what are they?

And also, just so that I'd know, how do I do what I wanted to do?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Eden Landau wrote:It's not a game, actually, but a dice rolling utility, though I see your point.
Any more specific structure tips? How should I split the methods, if at all?
To be more specific, what other way is of programming? I know it sounds silly, and I know there ARE other ways, but what are they?

Well, as I said above, you should start off by thinking about classes, not methods - the what, not the how. Java is an object-oriented language, so you need classes to do just about everything. There'll be plenty of time later to worry about how you're going to code your methods.

One technique for working out what classes you're likely to need is word analysis; and it works something like this:

Write down in English (or your native language) a description of what you want your program to do. You've already said that it's a "dice-rolling utility", but you need to flesh it out a bit more. Does it need to provide totals? Is it for analysing probabilities? What is a Dice?

It's sometimes called a "mission statement", and it's purpose is to answer the question "why am I writing this program?". And keep it short: two or three sentences is usually enough.
The other thing: most important (and probably the most difficult for you right now, because your head is full of code) - don't include anything about how you're going to write it. At this stage, you need to focus on the objectives, not the methodology. Thus:
"A program that displays the results of throwing a multi-sided Dice as many times as requested."
is much better than
"A program that uses a random-number generator to simulate throwing a multi-sided Dice as many times as requested."
It may seem obvious to you, but the stuff about the RNG is the how, not the what.

Once you've got a good mission statement for your program, go through it, analysing its words:
Nouns (eg, Die/Dice) usually end up being classes. You will probably also need a class for the program as a whole, so you might want to think about giving it a good name.
Verbs (eg, 'roll', 'display', 'total') usually end up being methods.
Facts or numbers (for example: "A Dice is assumed to be a regular polyhedron of between 4 and 20 sides" (*Edit, see below)) often indicate attributes that your program/system may have to store.

You'll find that this exercise gives you a lot to start out with before you've even written a line of code. If you fancy trying it out, I'll be more than happy to look at it tomorrow.

And also, just so that I'd know, how do I do what I wanted to do?

I'll let someone else answer that one, because I don't want to encourage you any further down the road you're on.

Winston

(*Edit) - BTW, you do know that there are only 5 Platonic solids? (See here). You might want to restrict yourself to those for the moment.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Refering to another class's instance of a different class, inside a class