2

In our company, we have a rule which says that all files must have a 8 letters prefix in their names. For instance, Blarghhh_MyFile.java. Because of Java's limitation that a public class must have the same name of its file, our classes have stupid names. For instance:

Blarghhh_MyClass myObject = new Blarghhh_MyClass();

Instead

MyClass myObject = new MyClass();

Does someone know a workaround to solve that?

twernt
  • 20,271
  • 5
  • 32
  • 41
hbelmiro
  • 987
  • 11
  • 31
  • 17
    Drop this s̶t̶u̶p̶i̶d̶ rule? – Tunaki Feb 17 '16 at 17:18
  • Is this prefix based on the directory, the team, or something like that? – Andrew Williamson Feb 17 '16 at 17:20
  • What is the reason for that rule? Having multiple public classes with the same name is not a problem, as long as they are not in the same package. – MTilsted Feb 17 '16 at 17:20
  • 12
    get a new job at a decent company – wero Feb 17 '16 at 17:22
  • 6
    Make the package name the 8 letter prefix. Name the classes like normal – OneCricketeer Feb 17 '16 at 17:22
  • 3
    Or, add your name to live in your VCS history forever and ever. – Tunaki Feb 17 '16 at 17:23
  • Perfect solution - your name is 8 letters, just use that as the prefix and feel good about typing it. – Andrew Williamson Feb 17 '16 at 17:24
  • MyClass_Blarghh() seems better – Petter Friberg Feb 17 '16 at 17:29
  • 1
    Possible duplicate of [C++ typedef in Java?](http://stackoverflow.com/questions/34419508/c-typedef-in-java) – Andy Turner Feb 17 '16 at 17:29
  • I don't suppose you could tell us the name of the company? – Gergely Bacso Feb 17 '16 at 17:33
  • nothing in the JLS mandates that class names equal file names. So you could hack `javac` to remove `Blarghhh_` when compiling. But then the problem arises again for the generated `.class` files – wero Feb 17 '16 at 17:33
  • Can you tell us the name of the company so we never send our resume to it? – Drew Kennedy Feb 17 '16 at 17:37
  • 2
    The question is closed - I have to put my answer as comment. You can define innerclasses to be able to put a class called `X` into `ASDASDASD_X.java`. Like this `public class ASDASDASD_X { public static class X {}}`. You can reference it like that: `import any.pkg.ASDASDASD_X.X; public class ASDASDASD_Y {public static class Y {private X referenceToX;}}` – slartidan Feb 17 '16 at 17:42
  • 2
    Suck it up and deal with it because you're the employee? There are lots of poor tired shivering developers out there who will gladly name their classes whatever their new boss wants. `ILoveMyNewEmployerSoooooMuchLikeReallyThisHuuuuuugeAmountOfLove_JSONLoader.java` – QED Feb 17 '16 at 17:45
  • 1
    @QED you're absolutelly right – hbelmiro Feb 17 '16 at 17:57

1 Answers1

1

You cannot have an alias for classes in Java. But you could simple create a new class and let it extend your old. Something like: MyLongNameClass.java

public class MyLongNameClass{
    //stuff
}

WorkingClass.java

public class WorkingClass {

    private void coolFunc(){
    MyClass mc = new MyClass();
    }


    private class MyClass extends MyLongNameClass{}
}

But i would rather type the whole name then do it that way.

Jodn
  • 314
  • 1
  • 11