0
Is a method a kind of object? If yes, of which class is it an instance of?
I mean, take method 'main' for example: public static void main(String[] args) { } What would method main be an instance of?
5 Respostas
+ 5
A method is a function. It is not an object. It, sometimes, has input, and normally returns a value.
+ 3
Why would you need to store a function in a var? Just ponder this:
A function is used to compact code used repeatedly. Instead, you could just write the same code each time you need it; however, it is much easier to just use a function to compact your code and make it easier to read. Now, consider what a function is. You name a function. To call the func, you use its name.
What is a variable? How do you use it? It has a name that you can use to access the variable. This is almost identical to the way a function was described above.
So, a func is kind of like a var that stores code. So, I can't really answer the question you gave, but I think that you are asking the wrong question.
I hope that I have helped expain things. If something didn't make sense, I'd be glad to explain it.
0
Yes. I know it is a function.
But, unlike C, Java is Object Oriented.
So, suppose I create a variable that should hold a function, this is only possible if the function has a type. Or would I create this variable as a pointer to void and assign it the place in memory where the function is? Or is it not possible at all? (I mean, for a variable to hold a function as its value.)
0
Lets try a different approach:
Lets suppose I want to create a method 'table' that receives a method, and two integers as arguments, and prints a table of values returned by this method in the given interval:
table(Math.sin, 0, 1);
table(Math.cos, 0, 1);
You see. The first argument must be stored in a var. The method 'table' will do exactly the same thing, but with the results of different methods. Inside, method table will call these methods through this var, which is a referece to a function.
0
As I read "Java, The Complete Reference" it seems that J.G.'s first answer is correct. I'm not sure yet, though.
At least I found that Java is not a pure Object Oriented language as I thought it was. I was kind of expecting Java to be something like "everything is an object".