0

This is my enmun class

public enum CSSFont {

    RezeptName("-fx-font: 22 calibri;"),
    RezeptNameClicked("-fx-font: 22 calibri; -fx-underline: true; -fx-text-fill: purple"),
    RezeptTab("-fx-font: 15 calibri;");


    private String font;

    private CSSFont(String s) {
        this.font = s;
    }

    public String getFont() {
        return this.font;
    }
}

As you can see I created a getFont() function to get the String of each CSSFont object. Is there a way to directly make String objects in an enum class(I need the String for setStyle() methods in JavaFX), so that I don't have to always write CSSFont.object.getFont() but rather CSSFont.object? I tried to let CSSFont extend String, but obviously enums can only implement interfaces. Or is the only solution to create a class with static (final) String attributes?

EDIT: Thanks everybody, it seems I wasn't really sure when to use enums and when not to, since I have only one attribute(String) and I don't even need enumaration or comparison of these enum objects, I will use a class with static final string attributes ;).

Dwagner
  • 237
  • 4
  • 11
  • 2
    It seems what you actually want is a set of String constants. Not en enum. – JB Nizet Jun 18 '16 at 12:10
  • 1
    Just `@Override toString()`... P.S. as `enum` constants are implicitly `public static final` standard naming conventions apply - i.e. `UPPER_UNDERSCORE` and not `PascalCase`. – Boris the Spider Jun 18 '16 at 12:14
  • @JB Nizet yeah, but isn't an enum exactly that? A fixed set of final attributes? A set of string constants in a seperate class was also my suggested solution above, but I rather wanted to use enumn, because that's actually what enum is there for isn't it? – Dwagner Jun 18 '16 at 12:14
  • Try psf String and relax. It will be like 3 times less boilerplate. – Mikhail Boyarsky Jun 18 '16 at 12:15
  • @New2HTML no, that's exactly not want an `enum` is. An `enum` is a set of value classes with some special properties - mainly the fact that it guarantees `==` on two of the same value. – Boris the Spider Jun 18 '16 at 12:15
  • 2
    No. An enum is a class. It defines a type. Its members are instances of this class. They are comparable and have an ordinal. If you don't need any of these features, but just a bunch of constant strings, define a bunch of constant strings. – JB Nizet Jun 18 '16 at 12:17
  • @Boris the Spider already tried, but in functions the toString() method doesn't seem to be called automatically except for Sysout maybe. I get the massage "the setStyle(String) is not applicable for CSSFont"... and I know about conventions, this is just a little project of mine :P – Dwagner Jun 18 '16 at 12:17
  • @OP, [JB Nizet](https://stackoverflow.com/users/571407/jb-nizet) is usually right, and this case is no exception. If you don't need enumeration, conversion from `String` to instance or guaranteed singleton properties then `psf String` will work better. – Boris the Spider Jun 18 '16 at 12:19
  • P.P.S. couldn't you use CSS files and classes? – Boris the Spider Jun 18 '16 at 12:24
  • @Boris trhe Spider "An enum is a set of value classes with some special properties" which I just called or meant by attributes(of the same class)... but I guess you are right about comparing and I don't need any ordinal values either, it just seemed more intuitive to me at first to use enum.. And what the hell does everybody mean by psf String??? – Dwagner Jun 18 '16 at 12:26
  • @New2HTML it often does make sense to use an `enum` - especially if you need to hang multiple properties off of it. But in this case what you really need is a `String`; so using an `enum` is overkill. Think of `enum` instances as anonymous classes extending the `enum` class. – Boris the Spider Jun 18 '16 at 12:28
  • May I s using a properties file instead of a group of psf strings? – Arturo Montaño Jun 18 '16 at 13:11
  • @ArturoMontaño that would be better indeed - I would suggest using `.css` files and CSS classes in this case. Why reinvent the wheel. I [already suggested that](https://stackoverflow.com/questions/37896745/enum-with-strings/37900560#comment63247127_37896745), but the OP ignored me... – Boris the Spider Jun 21 '16 at 08:55

2 Answers2

0

You can use something like this:

  public enum MyType {
     ONE {
        public String toString() {
           return "this is one";
     }
    },

     TWO {
       public String toString() {
        return "this is two";
      }
   }
 }

Test it using:

public class EnumTest {
  public static void main(String[] args) {
   System.out.println(MyType.ONE);
   System.out.println(MyType.TWO);
 }
}

Originally taken from here

Community
  • 1
  • 1
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • See the [other answer](http://stackoverflow.com/a/37896838/2071828). This is the same suggestion, and leads to the same problems. But it has the added disadvantage of also adding huge amount of boilerplate code. – Boris the Spider Jun 20 '16 at 10:49
-1

You can override toString method:

public enum CSSFont {

    RezeptName("-fx-font: 22 calibri;"),
    RezeptNameClicked("-fx-font: 22 calibri; -fx-underline: true; -fx-text-fill: purple"),
    RezeptTab("-fx-font: 15 calibri;");


    private String font;

    private CSSFont(String s) {
        this.font = s;
    }

    public String getFont() {
        return this.font;
    }

    public String toString(){
        return this.font;
    }
}

Then you can get font as follows:

CSSFont.RezeptName.toString()

ThiepLV
  • 1,219
  • 3
  • 10
  • 21
  • As the [OP pointed out](https://stackoverflow.com/users/2852865/new2html) if a method takes a `String` this will not work. This works if the `enum` is part of, for example, `String` concatenation. – Boris the Spider Jun 18 '16 at 12:20
  • 3
    You have to write `myMethod(CSSFont.VALUE.toString())` which is no different to what the OP already has; maybe even slightly _less_ descriptive. – Boris the Spider Jun 18 '16 at 12:23
  • 1
    Yeah, and in the Oracle API I think it says that the toString() method is supposed to rather return the name of the enum object than a string attribute... – Dwagner Jun 18 '16 at 12:37