10

I create a generic method without parameter, some thing like:

private <T> TableCell<T> createTableCell(){
return new TableCell<T>();
}

So, in my program, how to call this method for a concrete type?

Thinhbk
  • 2,194
  • 1
  • 23
  • 34
  • once you will call this method , it will return the TableCell of Type T ie T holds the type of the class – Abhishek Choudhary Jun 04 '12 at 05:14
  • 1
    Did you mean "return" instead of "Return"? – Ozair Kafray Jun 04 '12 at 05:15
  • Generally, when I call this method, it will return TableCell, but I want to cast it into a concrete type, called Class A and B. So how to achieve this? – Thinhbk Jun 04 '12 at 05:16
  • 1
    Possible duplicate of: http://stackoverflow.com/questions/590405/generic-method-in-java-without-generic-argument – anthony sottile Jun 04 '12 at 05:16
  • @Kafray: you're right, my typo. I updated it. – Thinhbk Jun 04 '12 at 05:21
  • @Sottile: that's an option, but not what you want. I can achieve this by adding a dummy parameter into my method, but that's not really make sense. – Thinhbk Jun 04 '12 at 05:24
  • In certain limited circumstances, Java *can* actually infer the correct type for T without all the extra syntax. In particular, if you're assigning the result to a variable of type `TableCell< SomeTypeOrTypeVariable >`, `TableCell< ? extends SomeTypeOrTypeVariable >`, or `Table< ? super SomeTypeOrTypeVariable >`, Java infers `SomeTypeOrTypeVariable` for `T`. If you are using `createTableCell()` as a top level call without using the result, Java infers `Object` for `T`. It's only when you try to nest the call to `createTableCell()` inside another expression that Java gives up. – Judge Mental Jun 04 '12 at 06:33

3 Answers3

22

Usually, the type is inferred, but you can specify the type with this syntax:

Note: You have an error in your method's definition - it had no return type:

private <T> TableCell<T> createTableCell(){
    return new TableCell<T>();
}

Here's how you can call it:

TableCell<SomeType> tableCell = myObject.<SomeType>createTableCell();


If you method doesn't access any fields, consider making it a static method, which you would call like:

TableCell<SomeType> tableCell = MyClass.<SomeType>createTableCell();


As an aside, when you use this syntax, many will marvel at your "eliteness" - it's a syntax not often seen.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • yes, your're right. This's my mistake. Anyway, the generic method is in a non-generic class, and I want other method of this class call this generic class, not method of other class. – Thinhbk Jun 04 '12 at 05:28
  • 2
    Yes. I also posted the correct answer first. (The other answer was edited after mine was posted - I think he copied mine). nm. – Bohemian Jun 04 '12 at 05:38
  • Thanks for your explanation, it helps me to understand clearer, as syntax in defining generic class vs generic method make me confused. – Thinhbk Jun 04 '12 at 05:39
  • Can you explain more about: "the type is infered", why calling createTableCell() does not work, while this.createTableCell() does? – Thinhbk Jun 04 '12 at 05:43
  • 2
    @Thinhbk The reason is technical: the java compiler does not accept statements that start with special character (in this case `<`), and would require a lot of changes in the compiler(grammar) to implement it. – Op De Cirkel Jun 04 '12 at 05:50
  • @Bohemian _>>>I think he copied mine_ --- Hmmm, you can easily check the diff of the edits. But i will give you my +1 since you need score so much – Op De Cirkel Jun 04 '12 at 05:55
  • @OpDeCirkel I went by the timestamp of my post vs the timestamp of the comment that altered you to your typo. You see that missingfaktor's comment was one minute *after* my post (which I got right first time). +1 to you too :) – Bohemian Jun 04 '12 at 06:01
  • @Bohemian Before discrediting somebody try to verify your argument – Op De Cirkel Jun 04 '12 at 06:06
  • @OpDeCirkel OK, I'm going to put things straight. Your edit to correct the typo was at `05:27:30`. My first post (which was correct) was at `05:26:02`, almost 1.5 minutes *before* your edit. Therefore you *may* have copied my correct answer. Are we cool? – Bohemian Jun 04 '12 at 06:54
7

Because the type can not be inferred from the context (when you call the method) you have to specify it when calling in the folowing way:

obj.<MyType>createTableCell()

where obj is the object of a class/type that contains that method.

Op De Cirkel
  • 28,647
  • 6
  • 40
  • 53
-2

You'd call this method the same way you called the constructor inside it: createTableCell<TypeName>().

Wormbo
  • 4,978
  • 2
  • 21
  • 41
  • Hmm, in that case you would have to extend the method signature to include a type hint. The usual way to do that if you don't really have anything relevant to pass into the method is a `Class` parameter that exists for the sole purpose of specifying the type. You'd then call the method as `createTableCell(TypeName.class)`. – Wormbo Jun 04 '12 at 15:41