Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Stefan Ram - 03 Dec 2005 18:48 GMT
 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>] ")"
Stefan Ram - 03 Dec 2005 18:59 GMT
>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>] ")"

Chris Smith - 03 Dec 2005 19:35 GMT
> >What I would understand would be:
> ><ClassInstanceCreationExpression> ::=
[quoted text clipped - 5 lines]
>
> "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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Stefan Ram - 03 Dec 2005 19:53 GMT
>public class Test<T>
>{
[quoted text clipped - 8 lines]
>}
>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.
Hendrik Maryns - 05 Dec 2005 12:52 GMT
Stefan Ram schreef:

>>public class Test<T>
>>{
[quoted text clipped - 14 lines]
>   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.

Signature

Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org

Thomas Hawtin - 03 Dec 2005 19:39 GMT
>   According to
>
[quoted text clipped - 15 lines]
> <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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.