Which is True?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zaaharhassan
    New Member
    • Nov 2008
    • 3

    Which is True?

    Which of the following statements are true about constructors and methods?

    A constructor has it's own name, a return type and is invoked using the new operator.
    A function has it's own name, a return type and is invoked using the dot operator.
    A constructor has the name of the class, no return type and is invoked using the new operator.
    1 & 2
    2 & 3
    1, 2 & 3

    None of the statements are true
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    And what do *you* think?

    kind regards,

    Jos

    Comment

    • bvmpriya
      New Member
      • Jul 2008
      • 8

      #3
      The answer is 2&3 are true

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by bvmpriya
        The answer is 2&3 are true
        Code:
        class B { 
           void mb() { ... } 
        }
        class D extends B { 
           void md() { ... }
           public void test() {
              mb(); // no dot
              md(); // no dot
           }
        }
        The dot isn't always needed ...

        kind regards,

        Jos

        Comment

        • myusernotyours
          New Member
          • Nov 2007
          • 188

          #5
          Code:
          class Foo{
          
          public Foo(){
          ...
          if(x == b)
          return; // Legal
          
          //If we are here continue doing stuff...
          ...
          ...
          
          }
          
          public void jump(){
          ....
          ....
          return; // Legal but not nessesarily nessesary.
          }
          
          public boolean sleep(){
          ...
          ...
          return true; // Mandatory
          }
          }
          So does the method jump have a return type?
          Void indicates only the absence of a return type. It's not a type. The compiler does not even expect us to return, because there is nothing to return.

          Can we therefore say the constructor Foo() and the method jump() are the same in terms of return types in that they both return nothing? The only thing perhaps would be that the compiler ALWAYS insists on a constructor returning nothing (void) so we don't even have to say. But a method may return a value.
          But then why did sun insist on using void where there is no Return Type in methods?? Why not do it just like for the constructor??

          Rgds

          Alex.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by myusernotyours
            But then why did sun insist on using void where there is no Return Type in methods?? Why not do it just like for the constructor??
            To keep the C and C++ programmers happy. There are many many things that
            could have been solved in another way but Java wants to look similar to those
            two other programming languages.

            kind regards,

            Jos

            Comment

            • myusernotyours
              New Member
              • Nov 2007
              • 188

              #7
              Originally posted by JosAH
              To keep the C and C++ programmers happy. There are many many things that
              could have been solved in another way but Java wants to look similar to those
              two other programming languages.

              kind regards,

              Jos
              Happy are the meek for they shall see god.

              rgrds

              Alex.

              Comment

              Working...