Nested Methods

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gmioannou
    New Member
    • Dec 2007
    • 8

    Nested Methods

    Can we create nested methods? (method within onther method, both returning data)
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by gmioannou
    Can we create nested methods? (method within onther method, both returning data)
    No you can not (the syntax of Java forbids it). You can however create a class
    local to a method and populate that inner class with methods but the use of it is
    little if all you want are nested methods. Why do you want them anyway?

    kind regards,

    Jos

    Comment

    • gmioannou
      New Member
      • Dec 2007
      • 8

      #3
      I had to create a function to return some value. I assume the practice of the language is to override the method within the subclass and return the value.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by gmioannou
        I had to create a function to return some value. I assume the practice of the language is to override the method within the subclass and return the value.
        Sure you can do that but those aren't nested methods (they don't exist in Java).

        kind regards,

        Jos

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by gmioannou
          I had to create a function to return some value. I assume the practice of the language is to override the method within the subclass and return the value.
          This is the syntax:

          [CODE=Java]class A {
          public void method() {...}
          }

          class B extends A {
          @Override
          public void method() {...}
          }[/CODE]
          (The annotation "@Override" is optional (and can only be used in versions >= 5) but documents the override.)

          Your description doesn't make me confident that you understand overriding or even that you need to use it. You should better describe your context, your goal and what you are trying to do.

          Comment

          Working...