Results 1 to 7 of 7

Thread: Why is this happening?

  1. #1

    Thread Starter
    Lively Member Sebouh's Avatar
    Join Date
    Jan 2005
    Posts
    73

    Why is this happening?

    Hi. I am having this problem. I know the solution but i want to know why the problem happened?

    i have this:
    if (obj.getClass() == Student.getClass())
    Student temp = (Student)obj;

    I get a compilation error, non-static methode getClass() cannot be referenced from a static context.

    i am using this code instead of: if (obj instenceof Student)
    When i use: if (obj.getClass() == getClass()) i don't have a problem.
    So can anyone please explain what the error means? Why is it saying i am calling from a static context. the method this code is in is not static. i am calling it from the equals method in the Student class.

    thanks.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Why is this happening?

    you must use the method "Object.equals" istead of "=="
    becuase "==" compares values to references
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Why is this happening?

    You could also do something like this :

    Code:
    		String classname=new String(P.getClass().getName());
    		if (classname.compareTo("Student")==0) {
    ...


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Why is this happening?

    Quote Originally Posted by manavo11
    You could also do something like this :

    Code:
    		String classname=new String(P.getClass().getName());
    		if (classname.compareTo("Student")==0) {
    ...
    There are so many ways to do this but yours is the worst

    Congratulations on the promotion by the way
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Why is this happening?

    Quote Originally Posted by ComputerJy
    There are so many ways to do this but yours is the worst

    Congratulations on the promotion by the way
    Where did I say that it's the best way to do it? All I know is that it will work, I'm sure there are better ways to do it. Why is it the worst? And what promotion?


    Has someone helped you? Then you can Rate their helpful post.

  6. #6
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Why is this happening?

    Quote Originally Posted by manavo11
    You could also do something like this :

    Code:
    		String classname=new String(P.getClass().getName());
    		if (classname.compareTo("Student")==0) {
    ...
    Looks good to me. You could actually override the compareTo() method and make it even more powerful in this case (in my opinion). It returns an int so you could deal with several different cases and it can also raise error messages that .equals() would simply shun aside by returning false.... I don't know if that's a plus or a minus

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Exclamation Re: Why is this happening?

    Quote Originally Posted by manavo11
    Where did I say that it's the best way to do it? All I know is that it will work, I'm sure there are better ways to do it. Why is it the worst?
    I know you didn't say, I just notified you it's the worst way
    because your not considering Time (Added unnecessary operations) and Space (Defined unnecessary variables)
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

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



Click Here to Expand Forum to Full Width