Results 1 to 7 of 7
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Why my cast has to be in println() method ?

    When I do this:

    Java Code:
    	@Override
    	public void update(Observable obs, Object arg1) {
    		System.out.println(((MyObservable) obs).getInt());
    		
    	}
    }
    I have no problems.

    But when I try this, the compiler complains:


    Java Code:
    	@Override
    	public void update(Observable obs, Object arg1) {
    		obs = (MyObservable) obs;
    		System.out.println(obs.getInt());
    	}
    }
    Help please?

    Thanks..

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why my cast has to be in println() method ?

    What is an Observable? And what is the compiler message you get with the second version?

    -----

    Just a guess... but you should remember that when you say obs.getInt() the compiler will look at how obs was declared to see if it has a getInt() method. Casting (or doing anything else to) obs won't change that. In the version that has no compiler message you are not calling getInt() on obs but rather on (MyObservable)obs and the compiler will check MyObservable to see if getInt() is defined there.
    Last edited by pbrockway2; 03-03-2012 at 10:09 PM.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why my cast has to be in println() method ?

    The first version is ok, the second one is the one that I am having trouble with.

    Observable is an Interface in Java API.

    MyObservable implements Observable.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why my cast has to be in println() method ?

    You can cast the object all you want, but this won't change what the variable is, and that's all the compiler sees. You must use a MyObservable variable there.

    Java Code:
        @Override
        public void update(Observable obs, Object arg1) {
            MyObservable myObs = (MyObservable) obs;
            System.out.println(myObs.getInt());
        }

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why my cast has to be in println() method ?

    Thank you.

    So no casting of variables..
    Only casting objects by the referencing variables..

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why my cast has to be in println() method ?

    Quote Originally Posted by fatabass View Post
    Thank you.

    So no casting of variables..
    Only casting objects by the referencing variables..
    No, you can cast variables, and that in fact is what your first version is doing.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why my cast has to be in println() method ?

    The first version is ok, the second one is the one that I am having trouble with.
    Sorry - I haven't had enough coffee this morning yet... I edited my post when I realised that. Or see Fubarable's comment.

Similar Threads

  1. How does System.out.println() method work?
    By fatabass in forum New To Java
    Replies: 4
    Last Post: 01-17-2012, 11:04 AM
  2. Cast error
    By nat45928 in forum New To Java
    Replies: 7
    Last Post: 04-26-2011, 02:48 AM
  3. Converting a println out in to a method
    By gcclinux in forum New To Java
    Replies: 9
    Last Post: 04-01-2011, 10:02 AM
  4. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 08:52 AM
  5. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-18-2008, 11:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •