5

I know this is simple, but I don't really understand the homework-question:

Assume the signature of the method xMethod is as follows. Explain two different ways to invoke xMethod:

public static void xMethod(double[] a)

I thought to invoke a method you just do:

xMethod(myarray);

What could it mean by asking two different ways?

A-Tech
  • 806
  • 6
  • 22
Eric Smith
  • 1,336
  • 4
  • 17
  • 32
  • Try and think of places to call the method from – Liviu T. Mar 12 '12 at 22:26
  • Not sure if that is the question, but because it is static, you can call it on an instance (`myClassInstance.xMethod()`) or on the class itself (`MyClass.xMethod()`)... – assylias Mar 12 '12 at 22:27
  • 5
    Since this is a beginners homework question, I assume your prof is quite wrong. Because, there is only one correct way of invoking it. – Martijn Courteaux Mar 12 '12 at 22:27
  • Stack overflow isn't really here to do your homework...best to try googling and/or reading texts on the matter. – RestingRobot Mar 12 '12 at 22:27
  • 1
    Give the context. This method could be for example invoked with `MyClass.xMethod(myarray)` or `MyClass instance = new MyClass(); instance.xMethod(myarray)`. – Jagger Mar 12 '12 at 22:27
  • @RyanAmos considering the question, I doubt this is the answer ;-) – assylias Mar 12 '12 at 22:28
  • Forget your confusion and believe only in reality. It's a wasting of your priceless time. – Lion Mar 12 '12 at 22:34
  • 2
    Please give a feedback what your teacher expected. In the Answers section you see man professionals a little bit lost with your homework. Even Jon Skeet is not answering. – Michael Konietzka Mar 12 '12 at 22:48
  • @MichaelKonietzka If Jon Skeet can't help us, no one can :/ – Ryan Amos Mar 12 '12 at 22:50

5 Answers5

10

For kicks, show your professor this:

XClass.class.getMethod("xMethod", a.getClass()).invoke(null, a);

Then tell them the two answers are

XClass.xMethod(a);
//and
xObject.xMethod(a); //this is bad practice
Ryan Amos
  • 5,422
  • 4
  • 36
  • 56
7

If this is for a first time java class, my guess is he is looking for these 2 cases:

//the one you have, using a precreated array
double[] myArray = {1.1, 2.2, 3.3}
xMethod(myarray);

//and putting it all together
xMethod(new double[]{1.1, 2.2, 3.3});

Basically illustrating you can make an array to pass, or simply create one in the call.

Just a guess though

dann.dev
  • 2,406
  • 3
  • 20
  • 32
  • I'm thinking so too... the other answers seem a bit complicated for what we have learned. Thank you – Eric Smith Mar 12 '12 at 22:43
  • the syntax is xMethod(new double[] {1.1, 2.2, 3.3}); – Hurda Mar 12 '12 at 22:48
  • Thanks for that, I can never remember the damn syntax for simple arrays! – dann.dev Mar 12 '12 at 22:52
  • That would actually be one way of invoking that method, since you call it the same way, but with different arguments. I'd suggest to look at the response below. http://stackoverflow.com/a/9675729/1513735 – Heskja Sep 30 '12 at 10:28
  • @Heskja I'd be tempted to agree with you, I put this answer in because the OP sounded like they we're doing a simple Java class, and wouldn't be near anything like the other answer, we were all hoping the OP would let us know what it was their professor was looking for but doesn't seem they have. – dann.dev Oct 01 '12 at 21:58
  • @dann.dev I just don't like the fact that OP acepted this answer as it's not correct, it my opinion. I don't like it when "bad" advice is given and accepted as the "correct" answer. – Heskja Oct 04 '12 at 17:57
3

You could invoke it either by calling it on a class, or via an instance of that class.

Foo.xMethod(a);

or:

Foo foo = new Foo();
foo.xMethod(a);

The first approach is prefered, but the second one will compile and run. But be aware that it is often considered a design flaw in the language that the second approach is allowed.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 1
    it is not correct to call it through class'es instance even though it wont break anything. – Amareswar Mar 12 '12 at 22:28
  • I think it's the same when compiled. I'd have to double check that, though. @Amareswar It's not a syntax error, it's just bad practice. – Ryan Amos Mar 12 '12 at 22:28
  • @Amareswar: Surpringly it's actually valid. It's just a bad practice to actually use it. But it's worth knowing about it in case you run into other people's code that uses it, so that you don't get totally confused by it. – Mark Byers Mar 12 '12 at 22:31
  • @MarkByers You are right. IDEs have default intentions set to highlight these kind of issues. – Amareswar Mar 13 '12 at 22:01
2

static methods are not bound to the construction of the class. The method above can be called either by constructing the class or just by using the namespace:

Classname myclass = new Classname();
myclass.xMethod(myarray);

or you could just do:

Classname.xMethod(myarray);

as you see, you don't have to construct the class in order to use the method. On the other hands, the static method can't access non-static members of the class. I guess that's what the question meant by 2 different ways...

dsynkd
  • 1,999
  • 4
  • 26
  • 40
0

There is only one valid way to do this:

YourClass.xMethod(myDoubleArray);

But, you can write non-totally-correct Java:

YourClass instance = new YourClass();
instance.xMethod(myDoubleArray);

This will work, but is considered as wrong. The Java compiler will even complain about it. Because a there is no need of invoking a static method by creating an instance of the class. Static means that the method is instance independent. Invoking it through an instance is pointless and confusing.

Later on, you will see that there is a second correct way of doing it, ie "reflection". But that is an advanced topic, so I assume you are not supposed to know this already.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • 1
    It's not actually incorrect, but it's a bad idea because it introduces a fake dependency -- it looks like `instance` is required and means something, but actually it isn't and doesn't. See http://stackoverflow.com/questions/7884004/is-calling-static-methods-via-an-object-bad-form-why – David Moles Mar 12 '12 at 22:34