1

I understand that in java you are able to write an external class that can be imported into a script and then run which allows that class to be used in multiple different places.

I was wondering if there is a way to do the same thing with a method. I find that sometimes I need to create an external class to do something very small and basic like the following.

simpleScript.java

public class simpleScript {

    public static void main() {

        // just a date variable which I will want to format
        // but I will always one of two formats every time
        // I use a date so I need a library/class function
        Date   dateNow = new Date();

        // to use my library/class function I have to
        // initialize it first
        dateFormat localDateFormat = new dateFormat();

        // now I can use it in the main only
        String   dmyNow  = localDateFormat.dmyFormat(dateNow);
        String   timeNow = localDateFormat.timeFormat(dateNow);

    }

}

dateFormat.java

public class dateFormat() {

    public static String dmyFormat(Date dateArg) {

        SimpleDateFormat dmyStyle = new SimpleDateFormat("dd-MM-yyyy");
        String dateResult = dmyStyle.format(dateArg);

        return dateResult;

    }

    public static String timeFormat(Date dateArg) {

        SimpleDateFormat timeStyle = new SimpleDateFormat("hh:mm:ss");
        String timeResult = timeStyle.format(dateArg);

        return timeResult;

    }

}

This doesn't bother me that much until it's something I need to use in multiple different methods, sometimes only for one date so i have to keep initializing the class to use the methods inside it (I don't want to make it a global class).

Is there another way to make my methods reusable without a class (across multiple java files)?

TheLovelySausage
  • 3,838
  • 15
  • 56
  • 106
  • You do not need to create instance of a class to call `static` methods. Take a look at this one - http://stackoverflow.com/questions/942326/calling-static-method-on-a-class – vk239 Feb 10 '16 at 15:45

3 Answers3

2

In the case you are talking about, static methods are a reasonable choice. Static methods/members belong to the class itself and not to any one instance. Some refer to this as belonging to all instances of the class, but that is not quite the same meaning.

Static methods are meant to be stateless (meaning that they don't depend on anything else but what they are given and will produce the same output every time for a given input). Like HopefullyHelpful stated, you call/access static members/methods by the classname, not the instancename, because they don't require an instance.

callyalater
  • 3,102
  • 8
  • 20
  • 27
0

You can call a static method with ClassName.functionName(parameters)

You don't need to initialize anything. You just need to import the class you are using. A classic example of that would be a library class like java.lang.Math, which only contains static methods.

public class simpleScript {
    public static void main() {
        Date   dateNow = new Date();

        String   dmyNow  = dateFormat.dmyFormat(dateNow);
        String   timeNow = dateFormat.timeFormat(dateNow);
    }
}
HopefullyHelpful
  • 1,652
  • 3
  • 21
  • 37
-2

What you are referring to is static methods. You can create a class with static methods that can then be called without creating an instance of a class.

Generally speaking, however, this is bad object-oriented programming design and indicates you need to re-think your design.

TangledUpInBlue
  • 746
  • 4
  • 13
  • 5
    Static classes/methods are not *bad* design, per se, but they are for a very specify use case (ie. a stateless algorithm library like java.lang.Math). – callyalater Feb 10 '16 at 15:47