• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why methods?

 
Ranch Hand
Posts: 1162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do we call it a method and not a function? what was wrong with the mathematical term that they had to come up with the word 'method'?.
Do you like it?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because a function in the mathematical sense is not exactly the same as what a method (or procedure) in a programming language is.

A mathematical function takes one or more input values and maps the inputs to some other value (the "return value"), without side effects.

In Java and other programming languages, methods or procedures don't always return a value (a method might be void) and can have side effects.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also, a mathematical function always returns the same output given the same input. a method might not.
 
Arvind Mahendra
Ranch Hand
Posts: 1162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
makes alot of sense. Thanks. We must be careful of such things.
 
Arvind Mahendra
Ranch Hand
Posts: 1162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fred rosenberger:
also, a mathematical function always returns the same output given the same input. a method might not.



What? How so?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A "why" method is a special form of "get" method. The return type is Explanation.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arvind Birla:


What? How so?



well, the random number generators come to mind. and constructors don't technically return the same thing each time either.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A constructor is not a method (nor is it a function), and a constructor doesn't return anything. A constructor is a special piece of code called to initialize an object.

The operation of a mathematical function is stateless (it doesn't depend on some hidden state), that's why it always returns the same output if you give it the same input. A mathematical function just maps input values to output values, and the output only depends on the input, and not on some state that is kept in memory somewhere.

For more info, see Functional programming.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
way too meaningful!!!
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:
A constructor is not a method (nor is it a function), and a constructor doesn't return anything. A constructor is a special piece of code called to initialize an object.



This has always been a beef of mine. I know i'm in the minority, but i think a contstructor DOES return something - the object it creates. afterall, you write

Foo myFoo = new Foo();

to me, that implies that the constructor is returning a reference to the newly created object, which then gets assigned to the myFoo reference. if i had another method:



what's the REAL difference? I personally would argue that a constructor is a special method, but i'm sure someone will argue with me.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A constructor does not contain a return statement and it doesn't have a return type, so it doesn't return anything. It's not the constructor that returns the new instance, it's the new operator that creates the instance, calls the constructor to initialize it, and then returns the object.

Have a look at section 8.1.6 of the JLS. Instance initializers, static initializers and constructors are separate from class member declarations (fields, methods, nested classes and nested interfaces). That means that a constructor is technically not regarded as a class member. Section 8.8 says it explicitly:

Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.


I agree that superfically a constructor may look like a special kind of method, but it really isn't if you look a little deeper.
[ June 11, 2008: Message edited by: Jesper Young ]
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic