f



"new" [<TypeArguments>] <ClassOrInterfaceType> "(" [<ArgumentList>] ")"

  According to

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.9

  the following production holds

<ClassInstanceCreationExpression> ::=
  "new" [<TypeArguments>] <ClassOrInterfaceType> "(" [<ArgumentList>] ")"

  What would be an example for a class instance creation expression
  with type arguments? Here's a reminder about those:

<TypeArguments> ::= 
  "<" <TypeArgument> {"," <TypeArgument>} ">"

  What I would understand would be:

<ClassInstanceCreationExpression> ::=
  "new" <ClassOrInterfaceType> [<TypeArguments>] "(" [<ArgumentList>] ")"


0
ram (2986)
12/3/2005 6:48:57 PM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

5 Replies
637 Views

Similar Articles

[PageSpeed] 39

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>What I would understand would be:
><ClassInstanceCreationExpression> ::=
>"new" <ClassOrInterfaceType> [<TypeArguments>] "(" [<ArgumentList>] ")"

  OK, this does not make sense, because "<TypeArguments>"
  already are part of "<ClassOrInterfaceType>" -- so what I
  understand would be:

"new" <ClassOrInterfaceType> "(" [<ArgumentList>] ")"
 
0
ram (2986)
12/3/2005 6:59:37 PM
Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
> >What I would understand would be:
> ><ClassInstanceCreationExpression> ::=
> >"new" <ClassOrInterfaceType> [<TypeArguments>] "(" [<ArgumentList>] ")"
> 
>   OK, this does not make sense, because "<TypeArguments>"
>   already are part of "<ClassOrInterfaceType>" -- so what I
>   understand would be:
> 
> "new" <ClassOrInterfaceType> "(" [<ArgumentList>] ")"

Does this help?

    public class Test<T>
    {
        public <E> Test()
        {
        }

        public static void main(String[] args)
        {
            Test<String> t = new <Number> Test<String>();
        }
    }

In the line of code in main, "<Number>" matches the TypeArguments in the 
production for ClassInstanceCreationExpression.  <String> matches the 
TypeArguments for ClassOrInterfaceType.  The former is a set of explicit 
type arguments on the constructor, not the class.

-- 
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
0
cdsmith (3862)
12/3/2005 7:35:59 PM
Stefan Ram wrote:
>   According to
> 
> http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.9
> 
>   the following production holds
> 
> <ClassInstanceCreationExpression> ::=
>   "new" [<TypeArguments>] <ClassOrInterfaceType> "(" [<ArgumentList>] ")"
> 
>   What would be an example for a class instance creation expression
>   with type arguments? Here's a reminder about those:
> 
> <TypeArguments> ::= 
>   "<" <TypeArgument> {"," <TypeArgument>} ">"
> 
>   What I would understand would be:
> 
> <ClassInstanceCreationExpression> ::=
>   "new" <ClassOrInterfaceType> [<TypeArguments>] "(" [<ArgumentList>] ")"

It's the same as the type arguments before a method. I can't quite think 
why you would want it. Potentially you might want two arguments, say, 
Comparable, but in a constructor?

     new <String>MyClass(strListA, strListB)

     public <T> MyClass(List<T> a, List<T> b) { ... }

Looks unlikely to me. Almost certainly you could use wildcard instead.

There appears to be a slight error in the stated grammar. javac does not 
accept wildcard types there. They wouldn't make any sense in that position.

Tom Hawtin
-- 
Unemployed English Java programmer
http://jroller.com/page/tackline/
0
usenet120 (1728)
12/3/2005 7:41:43 PM
Chris Smith <cdsmith@twu.net> writes:
>public class Test<T>
>{
>    public <E> Test()
>    {
>    }
>
>    public static void main(String[] args)
>    {
>        Test<String> t = new <Number> Test<String>();
>    }
>}
>Does this help?

  Yes. So, "Number" is the argument for the constructor's type
  parameter "E". I believe that I was able to grasp the
  formalism, though I would still will have to sit down and
  think about it in order to find an example where such a
  parameter is helpful.
0
ram (2986)
12/3/2005 7:53:20 PM
Stefan Ram schreef:
> Chris Smith <cdsmith@twu.net> writes:
> 
>>public class Test<T>
>>{
>>   public <E> Test()
>>   {
>>   }
>>
>>   public static void main(String[] args)
>>   {
>>       Test<String> t = new <Number> Test<String>();
>>   }
>>}
>>Does this help?
> 
> 
>   Yes. So, "Number" is the argument for the constructor's type
>   parameter "E". I believe that I was able to grasp the
>   formalism, though I would still will have to sit down and
>   think about it in order to find an example where such a
>   parameter is helpful.

I seem to recall to have seen something like this in the source code of 
HashMap.  Anyway, the same can be done when invoking methods against an 
object/a class, and unfortunately you really need it, as in:

public class SomeClass{
   public void someMethod(Set<String>){}
}

public class Main{
   public static void main(String[] args){
	someMethod(Collections.<String>emptySet())
   }
}

You really need the <String> there, otherwise the compiler will complain 
method is not applicable to the arguments Set<Object>.

See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6293352

HTH, H.

-- 
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
0
12/5/2005 12:52:16 PM
Reply: