• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

object-encapsulation vs performance / class-file-size

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Place it inside the object. If you create 10000 objects the method will only be loaded into memory once.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic