| Author |
object-encapsulation vs performance / class-file-size
|
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
I have a object and I am not sure to place a (long) method within this object to set some properties which only belongs to this object-instance.
What if I retrieve (for example, 100000) object-instances.
Would it be not better,
to place this (long) method out of that object-instance in a separate class
(for example, a business-class) and invoke it from that class?
In this case,
I only have one method which sets the properties of a instance instead of 100000 methods,
where each instance has this method in it.
However, when doing this, it disturbs my object-encapsulation.
What should I do?
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
Place it inside the object. If you create 10000 objects the method will only be loaded into memory once.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
|
There is not a copy of the byte code of a method for each instance of a class. If this would be so, it would be extremely wasteful - the byte code is the same for each instance and will never change. So having long methods does not mean that your objects are going to take up more memory, there's no reason to put long methods somewhere in a utility class.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: object-encapsulation vs performance / class-file-size
|
|
|