• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How do you instantiate this class?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have

what syntax is required to instantiate a class Hose object?


 
Marshal
Posts: 27371
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would depend on what constructors were available.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there no constructor, then compiler insert default no-argument constructor for your class.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect the question is really about the generic types. E.g. you can't exactly go:
new Hose<Hose<Hose<Hose<Hose<.. infinite number of Hoses.....>>>>>();
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't worked out a way of instantiating it directly (without cheating and omitting the generics). I'd be interested to see a solution to that. But I can create a non-generic subclass that can then be instantiated:
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can create a Hose object without type and with type. I think, if you create a Hose object without type, it will use Hose as E.

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthikeyan Kandasamy wrote:You can create a Hose object without type and with type. I think, if you create a Hose object without type, it will use Hose as E.


No, if you omit the type it's the same as leaving out the type when creating collections. You lose the type-safety completely.

The other approach you've used - creating a non-generic subclass - is the same as mine.
 
Karthikeyan Kandasamy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came to that conclusion because of this code snippet. I tried passing an object to the Hose constructor and it gave a compiler error.




Error:

GenericSamples7.java:11: cannot find symbol
symbol : constructor Hose(java.lang.Object)
location: class genericsamples.Hose
Hose h2 = new Hose(new Object());
^
1 error
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies, you're right.

If you don't give the generic type, then anywhere the compiler needs to know the type (like in that constructor) it uses the "erasure" of that type. Which, if the type has an upper bound, is that upper bound. So in this case, because it's E extends Hose, will be Hose.
 
No, tomorrow we rule the world! With this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic