Are there any advantages of making the method make

2019-07-29 01:02发布

问题:

package hf;

import javax.sound.midi.*;

public class BeatBox1 
{
    public static void main(String[] args)
    {
        BeatBox1 boom = new BeatBox1();
        boom.go();
    }

    public void go()
    {
        try
        {
            Sequencer player = MidiSystem.getSequencer();
            player.open();

            Sequence seq = new Sequence(Sequence.PPQ,4);

            Track track = seq.createTrack();

            for(int i = 5;i<125;i+=5)
            {
                track.add(makeEvent(144,i,i));
                track.add(makeEvent(128,i,i+2));
            }

            player.setSequence(seq);
            player.start();

        }
        catch(Exception e)
        {
            System.out.println("Problem starting the BeatBox");
        }
    }

    public static MidiEvent makeEvent(int onOff,int note,int time)
    {
        MidiEvent event = null;
        try
        {
        ShortMessage a = new ShortMessage();
        a.setMessage(onOff,1,note,100);
        event = new MidiEvent(a,time);
        return event;
        }
        catch(Exception e)
        {
            System.out.println("Error in creating Event.");
        }
        return event;
    }
}

I found the above example code in a book. They recommend making the makeEvent method static. What is the reason?

The program runs correctly when makeEvent() is made non-static as well. Is there any performance gain or any advantage that can be obtained by making the method static?

回答1:

Based on general OOP principles, a method is actually a 'behavior' which your class offers to public views of its instance. An instance is a real life version of the 'abstract existence' of the class. Now the important thing to understand here is that instance methods are behaviors that the class demonstrates. The behaviors of the class are tied to its 'state' ie. the variables or properties. For different values of state the same method of the class can show different behaviors i.e: operations performed.

Thus as a general rule: when a method offers a behavior that is dependent on the state of the instance and the behavior is unique to the class it should be non static and encapsulated in the class. Such a behavior should be exposed to the world only through a well defined contract, that normally is a public method or an interface of the class.

However when a method offers generic behavior not tied to any class or instance state nor influenced by the change in state it should be static i.e: independent of any class. Some examples are converting a given date to string, or loggin a message, or converting exceptions etc.

See what fits your case.



回答2:

The reason is that you can access to static method directly without creating instance of class



回答3:

It's more about style. makeEvent does not access any fields of BeatBox1, and static uses the compiler to ensure that claim.

As far as performance is concerned, there will be little to no difference. The complier will add a this as the first parameter of non-static methods, which is how fields are accessed, but this shouldn't make a real impact on performance.



标签: java static midi