Results 1 to 11 of 11

Thread: Conflict

  1. #1
    Join Date
    Aug 2013
    Posts
    55
    Rep Power
    0

    Default Conflict

    I was just using the instanceof operator. But I see some weird behavior when I used instanceof in two different circumstances and I am getting conflicting result. The line o instanceof Moof gives false inside main function but gives true inside the function func. Can somebody explain what is going on?

    Lines to look out for in the code are 7 an 14

    Java Code:
    public class Testing {
    	public static void main (String [] args) {
    		Moof one=new Moof();
    		Moof two=new Moof();
    		Object o=new Object();
    		boolean bool=o instanceof Moof;
    		System.out.println(bool);
    		one.func(two);
    	}
    }
    class Moof {
    	public void func(Object o) {
    		boolean bool=o instanceof Moof;
    		System.out.println(bool);
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,446
    Blog Entries
    7
    Rep Power
    20

    Default Re: Conflict

    Your main( ... ) method tests whether or not 'o' is a Moof (which it isn't) and the other method tests whether or not 'two' is a Moof (which it is). What's the problem?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Conflict

    Not sure what is so surprising here.

    In your main method you have created
    Java Code:
    Object o=new Object();
    it is "generic" object and have nothing with Moof class. So this instance o could not be an instance of Moof and thats why bool is false.

    In second part you are sending instance of Moof as an parameter to func() function, yes? So naturally, this object in func() is of type Moof, right?

  4. #4
    jmohandos304 is offline Member
    Join Date
    Apr 2014
    Posts
    79
    Rep Power
    0

    Default Re: Conflict

    ok, understood.

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,894
    Rep Power
    5

    Default Re: Conflict

    Quote Originally Posted by jmohandos304 View Post
    ok, understood.
    Prove it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jmohandos304 is offline Member
    Join Date
    Apr 2014
    Posts
    79
    Rep Power
    0

    Default Re: Conflict

    Tell me how to prove it to you that I understood?

    I will have to repeat the statement from JosAH to prove it to you.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,446
    Blog Entries
    7
    Rep Power
    20

    Default Re: Conflict

    Quote Originally Posted by jmohandos304 View Post
    Tell me how to prove it to you that I understood?

    I will have to repeat the statement from JosAH to prove it to you.
    But then you have to proof that I was right ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: Conflict

    Quote Originally Posted by JosAH View Post
    But then you have to proof that I was right ;-)

    kind regards,

    Jos

    Not at all. The great and powerful Jos speaks in Axioms. :)

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,446
    Blog Entries
    7
    Rep Power
    20

    Default Re: Conflict

    Quote Originally Posted by jim829 View Post
    Not at all. The great and powerful Jos speaks in Axioms. :)
    Mwah, I'd prefer to speak in dark and myserious riddles; nobody would really understand me but they all would be too afraid to protest against them; something like ict management does every day ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,894
    Rep Power
    5

    Default Re: Conflict

    Quote Originally Posted by jmohandos304 View Post
    Tell me how to prove it to you that I understood?
    By showing code that applies the newly found understanding of course :)

    You must have had SOME reason for posting that you "understand". I just thought to help you to actually push it through in stead of only passing a vague hint towards it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,446
    Blog Entries
    7
    Rep Power
    20

    Default Re: Conflict

    Quote Originally Posted by gimbal2 View Post
    By showing code that applies the newly found understanding of course :)
    Testing the validity of something can never be a proof of that something; one counter example would be enough to show its falsehood though.

    kind regards,

    Jos aka Your Free Sunday Nitpicker
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. hashCode function conflict
    By voipp in forum Advanced Java
    Replies: 8
    Last Post: 11-12-2013, 03:13 PM
  2. Filter url pattern conflict in web.xml
    By hemant_metalia in forum Advanced Java
    Replies: 0
    Last Post: 11-07-2011, 10:14 AM
  3. Inner class name conflict problem
    By ggyyree in forum New To Java
    Replies: 1
    Last Post: 12-10-2010, 09:10 PM
  4. Name Conflict
    By vanhuh21 in forum New To Java
    Replies: 7
    Last Post: 08-09-2010, 11:45 AM
  5. jdk jre tomcat conflict
    By simo_mon in forum Java Servlet
    Replies: 7
    Last Post: 09-29-2009, 08:44 AM

Posting Permissions

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