Basic Java Homework

I know this is simple, but I do not quite understand the question ...

Suppose the signature of the xMethod method is as follows. Explain two different ways to call xMethod:

public static void xMethod(double[] a)

I thought of calling the method you just did:

xMethod(myarray);

What does it mean by asking two different ways? Maybe I'm just looking at this question too much.

+5
source share
5 answers

If this is the first java class, I think it is looking for these two 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, you can make an array for transfer or just create it in a call.

Just a guess though

+7
source

For feet show your professor:

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

Then tell them that there are two answers:

XClass.xMethod(a);
//and
xObject.xMethod(a); //this is bad practice
+10
source

, .

Foo.xMethod(a);

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

, . , , .

+3

. , :

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

:

Classname.xMethod(myarray);

, . , . , ...

+2

:

YourClass.xMethod(myDoubleArray);

Java:

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

This will work, but is considered incorrect. The Java compiler will even complain about it. Because there is no need to call a static method, creating an instance of the class. Static means that the method is instance independent. Calling it through an instance is meaningless and confusing.

You will see later that there is a second correct way to do this, i.e. "reflection". But this is an extended topic, so I assume that you should not know this already.

0
source

All Articles