• Post Reply Bookmark Topic Watch Topic
  • New Topic

confused at what a method is?  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

i thought a method was a function on an object like

dog.barks      plane.flies et etc

is:
public static void Airplane(){
does something}

also a method...or is it a function? seems to be interchangeable...does it matter?

tanks
 
Stephan van Hulst
Saloon Keeper
Posts: 7964
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The words 'function' and 'method' are sometimes used interchangeably, but a method is different because it is associated with an object.

Non-static methods are associated with the object on which you call the method.

Static methods are associated with the class object of the type on which the method is defined.

Personally, in Java I like to use the word 'function' to refer to variables of a functional interface type. Functional interfaces are a new concept that was introduced in Java 8.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey steve,

Non-static methods are associated with the object on which you call the method.
so is this like object.method  -  when it applies to the specific object...like airplane.flying?

Static methods are associated with the class object of the type on which the method is defined.
is this MyMethod(){
code}

Personally, in Java I like to use the word 'function' to refer to variables of a functional interface type. Functional interfaces are a new concept that was introduced in Java 8.

"variables of a functional interface type. Functional interfaces are a new concept that was introduced in Java 8." can you just elaborate a little...its best to hear it from someone in the know tha google cryptic answers...

thanks for your help
 
Stephan van Hulst
Saloon Keeper
Posts: 7964
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon, not to be rude, but I prefer 'Stephan'.

Yes, when you call airplane.flying(), the object at that time that is associated with the method call is the object referred to by airplane.

As for static methods, by class object I mean the object that represents the type where the method is defined. For instance, SomeType.someStaticMethod() is associated with an instance of Class<SomeType>. You can get this instance using SomeType.class.

In Java 8, to facilitate what is called 'functional programming', they added functional interfaces to the language. A functional interface is an interface that has one abstract method. The prime example is java.util.function.Function. When you want to call a method that takes such a type as its parameter, you can pass an object in the same way you would before Java 8, but you can now also pass a lambda expression or a method handle. Whichever you pass, I like to refer to that parameter as a 'function'.

For more information, check our Java 8 forum, or check the Oracle tutorials for lambda expressions: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!