9 Replies Latest reply: Jun 23, 2008 1:09 PM by 796440 RSS

    can i do a boolean (such as .equals) to check the variable object TYPE?

    807589
      I have an object called 'policy', the object variable TYPE is of Proposal. Is there a way I can do a boolean such as a .equals to check if that object is a Proposal object?

      In other words.. how can i do something like this? >>

      Proposal policy = new Proposal()

      if (policy.equals(Proposal))
      {
      do this method
      }

      thanks.
        • 1. Re: can i do a boolean (such as .equals) to check the variable object TYPE?
          807589
          How will it be anything else? if your declaring policy as a Proposal then it will be a Proposal object why do you need to check it anyway?
          • 2. Re: can i do a boolean (such as .equals) to check the variable object TYPE?
            796440
            if (policy instanceof Proposal)
            But if you think you need to do that, there's a good chance you have a design flaw and should be using polymorphism instead.
            • 3. Re: can i do a boolean (such as .equals) to check the variable object TYPE?
              807589
              thanks a ton!
              if (policy instanceof Proposal)
              worked like a charm ;)
              • 4. Re: can i do a boolean (such as .equals) to check the variable object TYPE?
                796440
                I'm still curious why you need to perform this check.
                • 5. Re: can i do a boolean (such as .equals) to check the variable object TYPE?
                  807589
                  the reason i had to do this is because i was overriding an entire class basically.. (our policy issue program) with a proposa program (both do about the same thing).. i just used two different object types.. Proposal vs Policy. the program does error-checking and i don't need to check all the values on the proposal program since variables such as 'ApplicationSignedDate' don't matter. what i did was check the object type.. and used an if/else to get it to do what i wanted. thanks again!
                  if (policy instanceof Proposal)
                      {
                        d_value = get_date_value("ApplicationSignedDate", false);
                      }
                  else 
                      {
                        d_value = get_date_value("ApplicationSignedDate", move_forward);  //move_forward is for error-checking
                      }
                  if (d_value != null)
                        base_coverage.setApplicationSignedDate(d_value);
                  this is a servlet that i'm working with here. Universe U2->Java front end

                  Edited by: LuckY7 on Jun 23, 2008 9:02 AM
                  • 6. Re: can i do a boolean (such as .equals) to check the variable object TYPE?
                    796440
                    oops

                    Edited by: jverd on Jun 23, 2008 9:04 AM
                    • 7. Re: can i do a boolean (such as .equals) to check the variable object TYPE?
                      796440
                      LuckY7 wrote:
                      the reason i had to do this is because i was overriding an entire class basically.. (our policy issue program) with a proposa program (both do about the same thing).. i just used two different object types.. Proposal vs Policy. the program does error-checking and i don't need to check all the values on the proposal program since variables such as 'ApplicationSignedDate' don't matter. what i did was check the object type.. and used an if/else to get it to do what i wanted. thanks again!
                      if (policy instanceof Proposal)
                      {
                      d_value = get_date_value("ApplicationSignedDate", false);
                      }
                      else 
                      {
                      d_value = get_date_value("ApplicationSignedDate", move_forward);  //move_forward is for error-checking
                      }
                      if (d_value != null)
                      base_coverage.setApplicationSignedDate(d_value);
                      this is a servlet that i'm working with here. Universe U2->Java front end

                      Edited by: LuckY7 on Jun 23, 2008 9:02 AM
                      Are Policy and Proposal in a parent-child relationship, or do they both extend a common base class or implement a common interface? If so, then you should not be doing this. You should be just overriding a method, say, validate().

                      If they don't have a common base type, maybe they should.
                      • 8. they extend a common class >> HTML_NewBusiness
                        807589
                        basically the class HTML_Proposal extends the class HTML_NewBusiness.. the method that validates these fields is about 150 lines of code.
                        I thought about overriding the entire method, but I figured it would be easier/cleaner if I just changed the line of code based
                        on a boolean value. i am new to java so i am open to new ideas.. i just have never used the 'instanceof' before. both classes
                        run off the same set of interfaces. when you create a proposal.. it pretty much does the exact same thing that new business does
                        when we issue a policy, i used two different object types to distinguish which table (proposal vs policy) to use.
                        • 9. Re: they extend a common class >> HTML_NewBusiness
                          796440
                          LuckY7 wrote:
                          basically the class HTML_Proposal extends the class HTML_NewBusiness.. the method that validates these fields is about 150 lines of code.
                          I thought about overriding the entire method,
                          Yes, you should override the method. I'm not sure what you mean by overriding the "entire" method, or what you think the fact that it's 150 lines of code has to do with it. If there are parts of the validation that are common to both classes, you could break them out into one or more protected methods that both validations call.
                          but I figured it would be easier/cleaner if I just changed the line of code based
                          on a boolean value.
                          Why would that be easier? Everything you've said so far screams "Polymorphism!"