• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Covariant return

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

Can anybody please tell me , why does the commented line gives compiler error when i uncomment it.
When it is assigned to string like - String s = (String)o.method1();
it doesnt give any error. But when it is included as a single line statement
compiler says
CovariantTest2.java:19: not a statement
(String)o.method1();
^
1 error

EDIT by mw: Added Code Tags.
[ May 11, 2007: Message edited by: marc weber ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Elizabeth Jose:
...CovariantTest2.java:19: not a statement
(String)o.method1();
^
1 error...


Welcome to JavaRanch!

The method returns an Object reference, but you don't need to do anything with that return value. You can just call the method for its effect...

o.method1();

Alternatively, you can assign the return value to a variable...

Object myObj = o.method();

You can also explicitly cast of the reference to type String...

String myStr = (String)o.method();

An explicit cast like this is required for downcasting -- when it's not clear to the compiler that the type of object does, in fact, match the type of the variable. The cast is the programmer's way of saying, "Trust me, assigning this reference to this variable will be okay at runtime."

But when you're not assigning the reference to any variable, it doesn't make sense to explicitly cast its type, and so this is not a valid statement.
[ May 11, 2007: Message edited by: marc weber ]
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not too sure, what you are trying to do.

if you dont want to save the value returned by the method then simply say

o.method1();

You are trying to cast the return type to String and not assigning it to a String reference, then why are you downcasting it at the first place?
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic