• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java doubt  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am kinda confused on the the behaviour of the following code.
`````````````
class j { }
class j1 extends j{
void check(){
System.out.println("Jyoti");
}
}
public class test21 {
public static void main(String args[]) {
class Foo {
public int i = 3;
}
Object o = new Object();
Foo foo = (Foo)o;
//j j2=new j();
//j1 j3=(j1)j2;
//j3.check();
System.out.println("i = " + foo.i);
}
}
`````````````````````````````````
This compiles fines and results in "3" . But when i try using any class other than Object(remove the comments in the code) this gives runtime error.The concept looks the same. Ideally it should throw runtime when we cast to a subclass.Then why does it work for Object?

Any help is highly appreciated.

Regards,
Jyoti
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Handles, joke names, and "tden tden" won't work here. You can change your display name
here.
Thanks!

Now, on to your question: Object won't work either; it will give a runtime error just like any other type. Double-check your work (first deleting all the .class files you've generated just to be sure) and I'm sure you'll agree.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Which JDK are you using?
 
Jyo Samantaray
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the initial profile - have changed it. hope the new name complies with your standard.

Coming to my question - I tested it and it works..I am using JDK 1.5.

Here is the program once again(I have removed the extra classes)...

`````````````````````````````````
public class Test1234 {
public static void main(String args[]) {
class Foo {
public int i = 3;
}
Object o = new Foo();
Foo foo = (Foo)o;//I thought this would throw a runtime
System.out.println("i = " + foo.i);
}
}
```````````````````````````````````````

Thanks for your response.

Regards,
Jyoti
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've actually changed one enormously important thing in this version. In your original, you were creating a java.lang.Object, and trying to turn it into a Foo via a cast. This won't work!

Object o = new Object();
Foo foo = (Foo)o;

In your second version, you're creating a Foo, and holding it in an Object variable, then using a cast to tell the compiler that you know "o" is actually pointing to a Foo object: this is perfectly legal.

Object o = new Foo();
Foo foo = (Foo)o;

Now, if you change the second version to use any class besides Foo on the first line, you will indeed see a ClassCastException. You can turn a Foo into a Foo because, well, because it's a Foo.

Casts on reference types never change objects in any way. They only tell the compiler something it can't figure out for itself.
 
Jyo Samantaray
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot - this was my own mistake - was trying too many things and Forgot to change the Object to Foo..

Thanks once again for noticing the error

Regards,
Jyoti
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!