Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object-encapsulation vs performance / class-file-size

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Place it inside the object. If you create 10000 objects the method will only be loaded into memory once.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic