Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Generics

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a doubt in the below question the answer for this is B,D

My assumption for the answers was b,d,e

But e is not the right answer. Can you please help me understand this.

two options can fill in the blanks to make this code compile? (Choose all that apply.)

A. On line 1, fill in with <>.
B. On line 1, fill in with <T>.
C. On line 1, fill in with <?>.
D. On line 3, fill in with <>.
E. On line 3, fill in with <T>.
F. On line 3, fill in with <?>.
 
Saloon Keeper
Posts: 12893
280
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can only use generic type parameters if you've declared them. The generic type parameter T is not declared in the main() method. Static methods can not use the generic type parameter declared in the class header.

Even if you declared a generic type parameter in the main method like this:

Line 3 would still be invalid, because you can't assign a Generic<T> to a variable of type Generic<String>, because you don't know what the value of T is. It could be something other than String, such as Integer.
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic