This week's book giveaway is in the OCAJP forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Object data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object data" Watch "Object data" New topic

Object data

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
How the statement works

Object o=”stringvalue”

If I try the below I wont works

B b=new B();
B=”another value”;

Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1282

Hi Jacob,

because Object is the base class of all other classes in Java you can assign an instance of any class to a reference of type Object. Therefore you can of course assign the String object ”stringvalue” to it.

But as String is not a subclass of type "B" you obviously can't assign a String to a reference variable of type B.

I agree. Here's the link:
subject: Object data