-1

Today I was looking at some prewritten code for a project I am working on and stumbled across something I had never seen before: arguments to a constructor that consisted of the class' own methods. Here is an example

SampleFw sampleFramework = new SampleFw(getName(), getType());

In this case getName() and getType() are both methods defined only in SampleFw. What exactly happens when this call to the constructor is made?

  • 4
    You're not passing methods, you're passing the result of their execution. – Aaron Jun 09 '16 at 21:55
  • 1
    Is this code located inside a `SampleFw` method? You're not calling the new `SampleFw`'s methods; you're calling `this`'s methods, or possibly static methods. – user2357112 Jun 09 '16 at 22:05
  • We cannot really tell what's going on as you haven't shown where this one statement is located and what the code around it looks like. Out of context, this question cannot be completely answered. We need to know the class and method within which the statement is located. – Jim Garrison Jun 09 '16 at 22:29

1 Answers1

3

arguments to a constructor that consisted of the class' own methods

No, that's not what this code does. It calls getName() and getType(), and passes the values returned by these methods to the constructor. So it's equivalent to

String name = getName();
String type = getType(); // assuming it's a String
SampleFw sampleFramework = new SampleFw(name, type);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • The problem here is not that the answer does not help the OP, it is that the OP's question doesn't have enough context to be completely answered. – Jim Garrison Jun 09 '16 at 22:29
  • The OP asked one question and this answers that question. It's not JB's responsibility to read minds, although it might be good to throw out some guesses as to why the developer may have chosen to do things that way. – Jeutnarg Jun 09 '16 at 22:30