Abstract class to save resouces

M

mich

Would an abstract class with static methods help save resources? I am
thinking that taking a bunch of static methods out of a class that gets
instantiated several times would save some resources.
 
J

Jason Cavett

Would an abstract class with static methods help save resources? I am
thinking that taking a bunch of static methods out of a class that gets
instantiated several times would save some resources.

I'm taking a bit of a guess at this, but I would thank that it
wouldn't really save resources. Having a static method in an abstract
class lets you access that method statically (obviously), but if you
have to instantiate multiple children classes, you're going to still
have multiple references to these children. So, that reference size
is still going to be the same.

That's my theory - hopefully someone more knowledgeable can provide a
definitive answer.

P.S. I am curious, however, about static Objects within a class. I
would think, since there is only one reference, it would save some
resources. Of course, if it's updated in one spot, it's updated
everywhere, so that may not meet everybody's needs.
 
M

mich

I'm taking a bit of a guess at this, but I would thank that it
wouldn't really save resources. Having a static method in an abstract
class lets you access that method statically (obviously), but if you
have to instantiate multiple children classes, you're going to still
have multiple references to these children. So, that reference size
is still going to be the same.

In this case I am not creating any children classes. It would be a class
whose methods are called. by other classes, so I am thinking that it would
save some resources.
 
J

Jason Cavett

Even if it does save resources, this seems to fall squarely in the  
category of "premature optimization".

Yes. I definitely agree with this statement. More optimization will
come out of correct software design, correctness, etc. Making minor
optimizations up front will, most likely, not give you any noticeable
improvements in the end result. Design/write the code correctly and,
then, if an issue of performance comes up, use a profile to see where
you would get the most benefit of optimization.
 
M

Mark Space

mich said:
Would an abstract class with static methods help save resources? I am
thinking that taking a bunch of static methods out of a class that gets
instantiated several times would save some resources.

No, I don't see how it possibly could.

Why do you think that static methods in a class "that gets instantiated
several times" would save anything? It makes no sense. Methods --
static or otherwise -- are not copied into each object each time a new
one is created. That would be silly.


You should post a SSCCE showing exactly where and how you think
"resources" would be saved. I'm sure you're just thinking about it
wrong and a little example would clear it right up.
 
K

Kevin McMurtrie

"mich said:
Would an abstract class with static methods help save resources? I am
thinking that taking a bunch of static methods out of a class that gets
instantiated several times would save some resources.

Try it with -XX:+PrintClassHistogram or a profiler. I think you'll find
that methods have no impact on individual object sizes in any language.
Methods are part of the static class definition.
 
M

mich

Kevin McMurtrie said:
Try it with -XX:+PrintClassHistogram or a profiler. I think you'll find
that methods have no impact on individual object sizes in any language.
Methods are part of the static class definition.


Ok, I will do that but my theory was based on the following:

"An abstract class is a class that never has any instances. It is used
purely to provide a base class for other classes. It can have properties
defined and can participate as the source or target of relationships."

http://www.authormps.com/dnn/Concepts/Classes/Characteristics/tabid/129/Default.aspx


Since I'm not very bright I might very well be getting it all wrong
 
A

Arne Vajhøj

mich said:
Ok, I will do that but my theory was based on the following:

"An abstract class is a class that never has any instances. It is used
purely to provide a base class for other classes. It can have properties
defined and can participate as the source or target of relationships."

http://www.authormps.com/dnn/Concepts/Classes/Characteristics/tabid/129/Default.aspx

Why should the fact that you can not instantiate an abstract class but
only subclasses of it mean that static methods in an abstract class
use less space that static methods in a concrete class ?

Arne
 
A

Arne Vajhøj

mich said:
Would an abstract class with static methods help save resources? I am
thinking that taking a bunch of static methods out of a class that gets
instantiated several times would save some resources.

No.

Remember that OO is an abstraction.

When you get down to the metal, then the code and the
data are at separate locations in memory and the code
for methods only exist in one copy - static or not
(non static just mean that there is an extra implicit
argument to the call).

Arne
 
M

Mark Space

mich said:
Ok, I will do that but my theory was based on the following:

"An abstract class is a class that never has any instances. It is used
purely to provide a base class for other classes. It can have properties
defined and can participate as the source or target of relationships."

Please re-read my reply up thread.

I think we are all very curious how you got from this statement to
"abstract classes save resources." You have some large misconception
about Java or OO programming. If you can actually explain what your
thinking is, we can help you understand how it really works.
 
M

mich

Mark Space said:
Please re-read my reply up thread.

I think we are all very curious how you got from this statement to
"abstract classes save resources." You have some large misconception
about Java or OO programming. If you can actually explain what your
thinking is, we can help you understand how it really works.

Thanks, and with all the replies from people much smarter than myself I'm
going to work with a profiler (should have done that first!) and then try to
figure how off-the-mark I really am.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top