This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adherence to Object Orientedness

 
Justin Coombs
Greenhorn
Posts: 22
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it against object oriented design principles to declare a new object of type "A" from inside a method in class "B" in order to access the instance variables of class A?
For example, if I have

And then a game class

And a trimmed down main...

Would this be considered bad code or bad practice, or anything like that? If so, could you please recommend what topics I should learn in order to do what it is I'm trying to do, which is access another class object's instance variables and change their values without resorting to creating an entire object of that class just for the purpose.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not against OOD principles, but it's kind of pointless. If there's an instance of class A which you need access to, then creating another instance and accessing that one instead isn't going to help. Just have your controller pass a reference of the A instance you need to the B instance which needs it. Class B would have to have a constructor or method which accepts an A.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that particular example:

Is there any particular reason why you don't just use
?
They do the same thing, except your example created a new object that it never uses, so didn't need to be created.
 
Justin Coombs
Greenhorn
Posts: 22
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be a dumb question, but how would I use theRealInventoryManager directly in class Game?
Because if I just try to write theRealInventoryManager.setSword(true); from inside the game class it wont compile because its referencing an object that cant be seen from inside game
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Just have your controller pass a reference of the A instance you need to the B instance which needs it. Class B would have to have a constructor or method which accepts an A.
 
Justin Coombs
Greenhorn
Posts: 22
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I would need to turn this


Into this?


If this is the way to do it, is the thing I'm passing into the method a reference to the object or is it the object itself?

 
Justin Coombs
Greenhorn
Posts: 22
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disregard, I reread the replies indicating that it is a reference that is passed in that way.
Also I tested it out and everything works properly. Slowly but surely my 1 remaining braincell is catching on to this Java stuff haha
Thanks for your replies
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic