• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Generic Class instance Variables

 
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Doubts :
1. Why warning occurs for str1? (Line 02)
2. Why String variables cannot be instantiated directly? (Compilation Error occurs at line 5 and 6)
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case String is used as generic type that this class will be holding when properly instantiated. Usually you would just use T or E. To fix compile time errors, you have two options. Specify when you're using Javas String or the type of class you'll be holding (holding is used very loosely here). As in this makeover.



Note java.lang.String usage, where we expect a Java String.

Preferably you chose another type holder such as forementioned T or E:


I hope this helped.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's because it's using generics in a confusing way.

Where it says class Test<String>, this means "create a class with the generic type String, where String can stand for any class". Then, within the class, String is interpreted as "whatever the generic type is", instead of interpreting it as a shorthand for java.lang.String.

This is why we usually use T or something else that doesn't clash with a real class name. The class you've written is identical to this one:
And now it should be much more clear what's happening. Lines 3 and 4 are fine. On line 5 it's assigning a java.lang.String to an unknown type. It won't allow this without an explicit cast. This is what we're doing on line 2, which compiles but may throw a ClassCastException at runtime.

And on line 6 it's trying to instantiate a T directly, which isn't allowed (for a start, you don't know what constructors T will have).

Edit: snap (almost)
 
Dishi Jain
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,Now I get it.
It's because of confusion between what type variable is and what "String" is.
Thanks Buddies
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic