views:

111

answers:

4

I have created a pkg for my regular simple commands. They are non-static, to be more extensible, but somewhat more time-consuming to use because of creating object to use one. My other classes use them.

$ ls  *.java
CpF.java      IsBinary.java      RmLn.java        Tools.java
F2S.java      IsRoot.java        SaveToDisk.java  WordCount.java
Filters.java  ModRelativePaths.java  SetWord.java     WordNumber.java
Find.java     powerTools.java        Sort.java

Which option would you choose to use them easier?

  1. to develop a small interface like 'powerTools.java' for the pkg.
  2. to create a class with static methods for them.
  3. to add a static method to each class
  4. to stop overusing 'creating too many files' and centralising some?
  5. sthing else?

Static VS object-discussion

  1. How can you get outside methods work like in their own classes without referencing them with their class-name? Not class_name.method(), just method(). Outside methods are from separate pkgs.

Code not Working with Import-static with my class and Pkg

$ cat Test.java 
import static a.*;

public class Test
{
    public static void main(String[] args)
    {
        a.printHello();
    }
}
$ cat a/Hello.java 
import java.io.*;

public class Hello
{
    public static printHello(){System.out.println("Hello");}
}

$ javac Test.java 
Test.java:1: cannot find symbol
symbol: class a
import static a.*;
              ^
Test.java:7: cannot find symbol
symbol  : variable a
location: class Test
        a.printHello();
        ^
2 errors
$ sed -i '[email protected]@printHello@g' Test.java 
$ javac Test.java 
Test.java:1: cannot find symbol
symbol: class a
import static a.*;
              ^
Test.java:7: cannot find symbol
symbol  : method printHello()
location: class Test
        printHello();
A: 

I define a single class, G which has only static methods. So I call G.print G.sort G.find etc.

ddyer
+3  A: 

At first glance, I bet that none of them maintains any state and thus can be safely declared static. I also have the impression that each class has actually only one public method which does only one task at once. It also sounds like as if they are tied to a specific platform.

If all of above is true, then you have 2 options:

  1. Just group them all in a public final class PowerTools class with a private constructor (so that it cannot be instantiated) and public static methods, each doing the desired task. You can eventually use the original classname as method name.

  2. Define a public interface PowerTools so that you can implement LinuxPowerTools, WindowsPowerTools, MacPowerTools, etcetera accordingly and create an abstract PowerToolsFactory to return the desired implementation automatically based on the current platform (you can use the system property os.name to sniff the current platform).

If this is for pure hobby purposes, option 1 would suffice. But option 2 is more "professional", if you understand what I mean.

Update: as per your update, you can use the import static statement for this. Also see this Sun Tutorial.

E.g.

import static java.lang.System.*;

public class Test {
    public static void main(String... args) {
        out.println("foo");
        exit(-1);
    }
}

If you have a com.example.PowerTools class containing the public static methods, then you could import it as follows:

import static com.example.PowerTools.*;

public class Test {
    public static void main(String... args) {
        if (isBinary(file)) {
            // ...
        }
        saveToDisk(file);
    }
}
BalusC
@BalusC: about static. Extending classes, creating variation, building independent blocks, having second branch for PowerTools, eventually breaking things up to smaller pieces -- do not sound good for static. I had later such static mess that I hated because it was harder to fix it with a bug. For the time being, I think I will keep it separate, make them more independent, use more time in creating objects -- and when they are ready to combine automatically do it that way, let comp do the hard work. Not sure about second.
HH
Extending classes is only interesting if the extended class should have its own state/behaviour. Variation and breaking in smaller parts could be done by adding overloaded methods and/or methods wrapping the other methods. Look for examples in Apache Commons libraries, e.g. `IOUtils` (based on `java.io`), `DBUtils` (based on `java.sql`), and several in Commons Lang like `StringUtils` (based on `java.lang`).
BalusC
When you import pkg with pkg with static public methods, can you somehow stop writing the first referencing part like instead of "class_name.method() just "method()"? There is otherwise not much difference in writing "(new class_name()).method()". The last thing I have initialised in the begining so it becomes shorter like "s.method()", cannot see how static makes anything easier that way.
HH
Use `import static com.example.*;`. Also see [Sun Tutorial](http://java.sun.com/docs/books/tutorial/java/package/usepkgs.html#staticimport). I updated my answer accordingly.
BalusC
Sorry, the example in the comment was wrong. In your case you should use `import static com.example.PowerTools.*;`, thus including the classname which contains the static methods. Also see the tutorial.
BalusC
A: 

I would suggest that you group some of that functionality together into a single class. Then, expose your API via static methods or even just simply:

boolean isRoot = new DirectoryHelper().isRoot(someFile);
boolean isBinary = new DirectoryHelper().isBinary(someFile);
new DirectoryHelper().saveToDisk(someFile);

Yes, you are creating new objects each time, but unless you doing these operations tens of thousands of times then this overhead will never be noticed. And with the above method, you now have a class which is easier to test and easier to do dependency injection with.

Chris Knight
A: 

If these are command line tools meant to be used from a shell, put all the classes in a single JAR and write a short script to invoke each by name. Store the scripts on your path, e.g. ~/bin.

#!/bin/sh
java -cp your.jar pkg.Find "${@}"

#!/bin/sh
java -cp your.jar pkg.Sort "${@}"

etc. The "${@}" forwards any parameters.

trashgod