aspose file tools*
The moose likes Java in General and the fly likes Passing a object from one method to another method in another class... question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Passing a object from one method to another method in another class... question" Watch "Passing a object from one method to another method in another class... question" New topic
Author

Passing a object from one method to another method in another class... question

Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
Ok. I'm building a small game and trying to get a combat system up and running. Everything compiles, however, when I create a new NPC object named enemy and pass enemy around, it's somehow losing its information.

Here's the revelant code:


The output is 0. Although I'm thinking it should be 3, because the eny object has a body stat of 3. Using get/set methods is also returning zero.

What's going on?
[ December 17, 2007: Message edited by: Chris Young the second ]

Technology can never substitute for knowledge.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Hi,

The constructor written in your code



creates new local variables and assign the argument values to these local variables only. The properties (fields) of class NPC remain unaffected.

Try changing the constructor to



Have a blasting time with your game!

- Aditya
Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
Thanks.

That worked.

However, I don't quite understand the "this" key word. How does it work? What does it do? I didn't have a problem with constructors set up the way I had them before when working with stuff in the same class. Is this due to the local variable thing (I thought passing an object to another class would retain the info in that object? Or am I just confused on that).

Also, speaking of .this, what is the "super" key word for?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Chris Young the second:


However, I don't quite understand the "this" key word. How does it work? What does it do?


Well, the keyword 'this' means the 'the current object in execution. That means the object using which you invoke this method, will be referred in the method.

That's when the 'this' helps assigning the incoming values to the actual set of variables of the object which is in execution.



Is this due to the local variable thing (I thought passing an object to another class would retain the info in that object? Or am I just confused on that).


yes very much. If you create a new local variable ["the presence of datatype in front of the variable name decides whether it is a brand new local variable or not"], it hides the instance variables and hence the effects you have faced initially when posting this question.


Also, speaking of .this, what is the "super" key word for?


The 'super' keywords is used to refer to the members of the 'immediate super class' of the present class.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
Thanks.

So, if an object is to be used within one particular class, local variables can be used?

Or is "this" supposed to always be used?

Because I have a perfect shuffle program that uses a card object and returns its starting card number in the deck as a string when its toSrtring() is called (because the default toString was giving me the object name and memory addresses, which wasn't really helpful), and it works fine.

Anyway thanks. This was a roadblock for me and I wanna get a good system down in the game before I expand the world too much (because a non-interactive world is just empty inside...)
Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
also, before I make some other silly mistake, are constructor variables ever private?

I mean, I understand that there are classes that do things that require private variables, and so I get all that stuff, but objects are a relatively new thing my teacher is starting to show us (even though we've been using them since day one, with the BufferedReader and InputStreamReader classes).

Also, when an object contains an object (HAS-A relationship), how does that work? I understand inheritance (like a supercomputer being a computer, or socks being footwear) but not an object (like a car) containing another object (like an engine).

How does HAS-A interact?
[ December 17, 2007: Message edited by: Chris Young the second ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Are constructor variables private? Not sure what that question means.

The parameters passed, or any local variables created in the constructor, have scope in the constructor only (just as they would have in a method). So they vanish after the constructor has completed (but the calling class may still have the fields used to pass to the constructor).

You usually give private access to all fields of your class; this means other classes can only access them via methods: a part of information hiding. Local variables don't have access, so you never say "private" or "public" about local variables.

The fields of a class are implicitly "this.something" but you can miss out the "this." if you don't have anything else with the same name. If you have a field called "number" and a local variable [or parameter] called "number" then writing "number" alone will give you the local variable [or parameter] and "this.number" gives you the field.

You can write a constructor like this. Some people will disagree, but this is how Eclipse sets up a constructor by default, and I think it is a good way to do it:NOT

As for an object has-another object, look at a Car:Note that you don't need to initialise speed; it defaults to 0. You can also give the fields private access if there are suitable methods which the subclasses have access to.
Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
what I meant by consturctor variables being private was can the instance variables for a class be private and still construct. However I have answered my own question on this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
It's always good when people work out the answers for themselves. But every class has access to its own private members, so we usually make fields private.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing a object from one method to another method in another class... question