aspose file tools*
The moose likes Java in General and the fly likes Adherence to Object Orientedness Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Adherence to Object Orientedness" Watch "Adherence to Object Orientedness" New topic
Author

Adherence to Object Orientedness

Justin Coombs
Greenhorn

Joined: Oct 10, 2012
Posts: 16

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-
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

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

Joined: Apr 06, 2010
Posts: 4244
    
    7

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

Joined: Oct 10, 2012
Posts: 16

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
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

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

Joined: Oct 10, 2012
Posts: 16

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

Joined: Oct 10, 2012
Posts: 16

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adherence to Object Orientedness
 
Similar Threads
Access an object via reference variable (or not)
help with sort code
Adding a new array element is not saving to memory
Trouble with arrays / Validating user input
Why is my type unexpected? required variable found value