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.
Nothing is withheld from us what we have conceived to do. -Russel Kirsch-
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.
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
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