3

I'm not Java expert but I'm curious. Suppose that I have this:

String StringRepeater(String s, Qty q) {
    String S = "";
    for(int i = 0; i<q;i++) {
        S +=s;
    }
    return S;
}

boolean MyComp(int a, int b) {
    return (a<b);
}

I want to replace the before code by respective Java code. I don't want to use static class.

It's possible to implement it, but not using a static class/singleton, I read that Java allows to implement this code (different to OOP) in structured programming maybe (Wikipedia).

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • Out of curiosity what exactly is `String StringRepeater(..)`? I know it's a method because you have the return type. The lack of `private`/`public` is confusing me a bit. – theGreenCabbage Dec 09 '13 at 15:41
  • 1
    Private/Public belongs to OOP Concepts, I want to implement this without class, the function (not methods) are example of functions that to do something... –  Dec 10 '13 at 02:35

2 Answers2

1

If you don't have side effects or mutable state then there's nothing wrong with having a static method. See the java.lang.Math class for an example.

This might look like:

public final class Util { // final disallows inheritance

    private Util() {} // making constructor private disallows instantiating this

    public static String repeat(String s, int times) {
        StringBuilder builder = new StringBuilder("");
        for (int i = 0; i < times; i++) {
            builder.append(s);
        }
        return builder.toString();
    }
}

This is the usual non-OO procedural approach. Java doesn't have a way to create independent functions that don't belong to a class.

You could take this static method and create an EL function for it (see this answer). and that would give you a function that you could call in the JSP. Or you could call the method in a controller and populate a request attribute with the returned value.

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Isn't this exactly the opposite of what is being asked? *"but not using a static class/singleton"* – Bart Apr 04 '14 at 19:30
  • @Bart: hard to say, the question could be clearer. the OP seems to want a non-OOP, "structured programming" way of creating a function he can call from his jsp. the answer explains he can't do without a class, and suggests creating an EL function that would let him create the function he wants. so I think it addresses what he needs. – Nathan Hughes Apr 04 '14 at 20:44
0

It's possible to implement it, but not using a static class/singleton, I read that Java allows to implement this code (different to OOP) in structured programming maybe(Wikipedia).

Another way of implementing "structural programing" in java is using JNI. Using JNI you can communicate java applications with modules written in another languages (C, C++, Python, Javascript...). It's complicated but some times is necessary.

Take a look to these links:

http://www.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

Can I call Java from Node.js via JNI and how?

Regards,

Community
  • 1
  • 1
Rodmar Conde
  • 956
  • 1
  • 12
  • 24