Abstract class to save resouces

Discussion in 'Java' started by mich, May 23, 2008.

  1. mich

    mich Guest

    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.
     
    mich, May 23, 2008
    #1
    1. Advertising

  2. mich

    Jason Cavett Guest

    On May 23, 9:56 am, "mich" <> wrote:
    > 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.
     
    Jason Cavett, May 23, 2008
    #2
    1. Advertising

  3. mich

    mich Guest

    "Jason Cavett" <> wrote in message
    news:...
    On May 23, 9:56 am, "mich" <> wrote:
    >> 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.


    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.
     
    mich, May 23, 2008
    #3
  4. mich

    Jason Cavett Guest

    On May 23, 12:53 pm, "Peter Duniho" <>
    wrote:
    > 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.
     
    Jason Cavett, May 23, 2008
    #4
  5. mich

    Mark Space Guest

    mich wrote:
    > 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.
     
    Mark Space, May 24, 2008
    #5
  6. In article <w2AZj.7950$>,
    "mich" <> wrote:

    > 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.

    --
    Block Google's spam and enjoy Usenet again.
    Reply with Google and I won't hear from you.
     
    Kevin McMurtrie, May 24, 2008
    #6
  7. mich

    mich Guest

    "Kevin McMurtrie" <> wrote in message
    news:...
    > In article <w2AZj.7950$>,
    > "mich" <> wrote:
    >
    >> 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.



    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
     
    mich, May 24, 2008
    #7
  8. mich

    Arne Vajhøj Guest

    mich wrote:
    > "Kevin McMurtrie" <> wrote in message
    > news:...
    >> In article <w2AZj.7950$>,
    >> "mich" <> wrote:
    >>> 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.

    >
    >
    > 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
     
    Arne Vajhøj, May 24, 2008
    #8
  9. mich

    Arne Vajhøj Guest

    mich wrote:
    > 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
     
    Arne Vajhøj, May 24, 2008
    #9
  10. mich

    Mark Space Guest

    mich wrote:

    > 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.
     
    Mark Space, May 24, 2008
    #10
  11. mich

    mich Guest

    "Mark Space" <> wrote in message
    news:eU0_j.440$...
    > mich wrote:
    >
    >> 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.


    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.
     
    mich, May 25, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler
    Replies:
    1
    Views:
    443
    R.F. Pels
    May 22, 2005
  2. Kris
    Replies:
    2
    Views:
    332
    Victor Bazarov
    Jan 18, 2006
  3. Replies:
    4
    Views:
    822
    Rolf Magnus
    May 17, 2006
  4. Replies:
    3
    Views:
    11,375
  5. Replies:
    0
    Views:
    128
Loading...

Share This Page