1

I have and Enum for my intent Extras I like to use in my android app. The type defenition is written like this:

public enum Extras { TITLE, ID_USER } // ... and some more Extras

in my code I like to use the Enums like this:

Intent i = new Intent(); 
i.putExtra( Extras.TITLE  , "My Title String");
i.putExtra( Extras.ID_USER, 5);

And

 String title = i.getStringExtra(Extras.TITLE);
 int userId   = i.getIntExtra(Extras.ID_USER, -1);

as I know enums have a toString method, my Enum Extras.TITLE should be automatically be casted to the String "TITLE".

Its even working if I print it to screen like this

System.out.println("Extra="+Extras.TITLE); 

But for the Intent Methods I get the error message:

The method putExtra(String, CharSequence) in the type Intent is not applicable for the arguments (Extras, CharSequence)

I could avoid this error if manually convert the enum to String like this:

i.putExtra( Extras.TITLE.name()  , "My Title String");
i.putExtra( Extras.TITLE.toString()  , "My Title String");

But I like to know why to String issn`t called automatically when I pass my Enum as parameter to this method ?

Is there a way to to pass my Enums without calling toString() ?

UPDATE: I tried to change my Enum to implement CharSequence interface. After this modification I can pass my Enums to methods which expect CharSequence parameters. But it is still incompatible with String methods.

public static enum Extras implements CharSequence { TITLE, ID_USER; // ... and some more Extras

    @Override  public char charAt( int index )                       { return this.toString().charAt(index);           }
    @Override  public int length()                                   { return this.toString().length();                }
    @Override  public CharSequence subSequence( int start, int end ) { return this.toString().subSequence(start, end); }
}
Radon8472
  • 4,285
  • 1
  • 33
  • 41
  • 3
    "as I know enums have a toString method, my Enum Extras.TITLE should be automatically be casted to the String "TITLE"" - how would you expect that to happen automatically? Which Java language rule would you expect to do this? There's no string concatenation here, which is all that calls `toString` automatically... – Jon Skeet May 22 '15 at 12:32
  • Everything but primitives has a toString() method. Note that there is no casting going on here: it's merely a method call that provides a string representation. – Jeroen Vannevel May 22 '15 at 12:34
  • 1
    If you really, really hate typing the `.toString()`, use automatic conversion by prefixing `""+` – tucuxi May 22 '15 at 13:33
  • The point I don`t unterstand is, why is toString not called automatically when my Enum is passed to a function wich expects a String parameter. – Radon8472 May 22 '15 at 13:40

1 Answers1

1

Nope, Enums in Java are Integer values. You cannot cannot get the String value without calling toString() method. See here and here for further info.

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • I have already seen both of this posts, but they were not helpfull. I yust like to know why toString is call automatically when i print it to screen, but not when I pass my Enum to a function. – Radon8472 May 22 '15 at 12:51
  • @Radon8472 It's not called _automatically_. There's an `Object` version which is what responds to the `Integer`, and _in the code of that method_ they manually call `.toString`. – Nic Oct 09 '18 at 21:44