This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
BWA HA HA HA HA HA HA! Tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!