Static vs non static methods - best practice

Discussion in 'Java' started by m97rek, Nov 2, 2007.

  1. m97rek

    m97rek Guest

    I have a question with regards to program design.

    If I have a class that looks something like:

    public class SampleApp extends Engine implements Listener_OnMyEvent
    {

    public static void main(String argv[])
    {
    int x=10;

    //Call metod1
    Method1(x);
    }

    public method1(int i)
    {
    //Call a method from the Engine Class
    //I know I don't have to have "Engine." here but it's just to
    clearify
    Engine.method2();
    }
    public void OnMyEvent()
    {
    Log("Event Hapended");
    }
    }

    Let's say now that I want to move method1 to another class/file in
    order to keep the code as neat and tidy as possible.

    How should I do that? What is best practice according to you? Should
    the methods there be static?

    I tried to create:

    public class MyMethods
    {
    public static method1(int i)
    {
    Engine.method2();
    }

    }
    ....but then method2 isn't available since Engine isn't implemented
    unless I pass the whole class along:
    MyMethods(x, this)

    Is this correct?

    I hope you see what I'm getting at here.
    Any suggestions is appreciated.

    Thanks
     
    m97rek, Nov 2, 2007
    #1
    1. Advertising

  2. m97rek

    Daniel Pitts Guest

    m97rek wrote:
    > I have a question with regards to program design.
    >
    > If I have a class that looks something like:
    >
    > public class SampleApp extends Engine implements Listener_OnMyEvent
    > {
    >
    > public static void main(String argv[])
    > {
    > int x=10;
    >
    > //Call metod1
    > Method1(x);
    > }
    >
    > public method1(int i)
    > {
    > //Call a method from the Engine Class
    > //I know I don't have to have "Engine." here but it's just to
    > clearify
    > Engine.method2();
    > }
    > public void OnMyEvent()
    > {
    > Log("Event Hapended");
    > }
    > }
    >
    > Let's say now that I want to move method1 to another class/file in
    > order to keep the code as neat and tidy as possible.
    >
    > How should I do that? What is best practice according to you? Should
    > the methods there be static?
    >
    > I tried to create:
    >
    > public class MyMethods
    > {
    > public static method1(int i)
    > {
    > Engine.method2();
    > }
    >
    > }
    > ....but then method2 isn't available since Engine isn't implemented
    > unless I pass the whole class along:
    > MyMethods(x, this)
    >
    > Is this correct?
    >
    > I hope you see what I'm getting at here.
    > Any suggestions is appreciated.
    >
    > Thanks
    >

    Actually, static means it IS accessible by Engine.method2();

    Though in general an abundance of static methods suggest that your OO
    design has a flaw.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Nov 2, 2007
    #2
    1. Advertising

  3. m97rek

    Lew Guest

    m97rek wrote:
    >> If I have a class that looks something like:
    >>
    >> public class SampleApp extends Engine implements Listener_OnMyEvent
    >> {
    >>
    >> public static void main(String argv[])
    >> {
    >> int x=10;
    >>
    >> //Call metod1
    >> Method1(x);


    You don't show us any method "Method1(int)". What is that method?

    >> }
    >>
    >> public method1(int i)
    >> {
    >> //Call a method from the Engine Class
    >> //I know I don't have to have "Engine." here but it's just to
    >> clearify
    >> Engine.method2();
    >> }
    >> public void OnMyEvent()
    >> {
    >> Log("Event Hapended");
    >> }
    >> }
    >>
    >> Let's say now that I want to move method1 to another class/file in
    >> order to keep the code as neat and tidy as possible.


    That's not an engineering reason. Methods go with the class whose behavior
    they implement.

    >> How should I do that? What is best practice according to you? Should
    >> the methods there be static?


    Static methods are for class-wide behaviors. It's not about making things
    "neat and tidy". If a behavior belongs to the whole class, as with utility
    classes or factory methods, then make the method static, otherwise make it
    instance-level. If you're not sure, and the class is instantiable, make it an
    instance method.

    Methods reflect your object model. If your model says that there is a
    business object type "Foo", and that Foo thingies do certain behaviors, then
    you implement that with a class Foo and methods in that class implement those
    behaviors (preferring instance level over static where feasible).

    >> I tried to create:
    >>
    >> public class MyMethods
    >> {
    >> public static method1(int i)
    >> {
    >> Engine.method2();
    >> }
    >>
    >> }
    >> ....but then method2 isn't available since Engine isn't implemented
    >> unless I pass the whole class along:
    >> MyMethods(x, this)


    Huh? You lost me on this point. What is Engine?

    Since its name begins with an upper-case letter, it must be a class. Since
    you invoke method2() (no arguments) via Engine, it must be a static method.

    MyMethods(x, this) is a constructor, but you haven't shown us this constructor.

    >> Is this correct?


    No.

    >> I hope you see what I'm getting at here.


    No.

    You really, really need to read and follow
    <http://www.physci.org/codes/sscce.html>

    Provide us with an SSCCE. If you can't compile the code then we can't either.

    Daniel Pitts wrote:
    > Actually, static means it IS accessible by Engine.method2();
    >
    > Though in general an abundance of static methods suggest that your OO
    > design has a flaw.


    --
    Lew
     
    Lew, Nov 2, 2007
    #3
  4. m97rek

    Are Nybakk Guest

    m97rek wrote:
    > I have a question with regards to program design.
    >
    > If I have a class that looks something like:
    >
    > public class SampleApp extends Engine implements Listener_OnMyEvent
    > {
    >
    > public static void main(String argv[])
    > {
    > int x=10;
    >
    > //Call metod1
    > Method1(x);
    > }


    You shouldn't call methods from main like this. Since main is static,
    all methods it calls must also be static. Let SampleApp have a
    constructor that you call. Do method calls with the object:

    public static void main(String args[]) {
    int x = 10;

    SampleApp newSampleApp = new SampleApp();
    newSampleApp.method1(x);
    }

    public SampleApp() { ... }

    >

    *snip*
    > I hope you see what I'm getting at here.
    > Any suggestions is appreciated.
    >


    I have no idea what you want. Please clarify.

    > Thanks
    >
     
    Are Nybakk, Nov 3, 2007
    #4
    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. Oliver Wong
    Replies:
    14
    Views:
    1,654
    Chris Uppal
    Jun 13, 2006
  2. pintu

    Static V/s Non Static Methods

    pintu, Jun 27, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    539
    active
    Jun 28, 2007
  3. Dobedani
    Replies:
    0
    Views:
    386
    Dobedani
    Jul 15, 2008
  4. o_ssie
    Replies:
    0
    Views:
    324
    o_ssie
    Apr 9, 2009
  5. Chuck Remes
    Replies:
    5
    Views:
    120
    Robert Klemme
    Oct 21, 2010
Loading...

Share This Page