You have a few misconceptions there.
First, there is a method definition, and then there is a method invocation.
Method definition is where you declare your method. You give it modifiers such as public
/private
/protected
, a return type, a name, a list of parameters, an optional throws
and a body:
public static int myInt( double myParameter ) {
return (int)myParameter;
}
Here, the parameters must have names. Otherwise, you would not be able to refer to them in the body.
And then there is the method invocation. Within some other method, like main
, you call your method:
int a = myInt( 15.7 );
You passed 15.7
without giving it a name. The value that you actually pass in a method invocation is called an argument as opposed to a parameter, which is the formal name and type given in the method definition.
If your method definition included a parameter of the type SuperClass
, it would look something like:
public static void myMethod( SuperClass myParameter ) {
...
}
You can't use new
in a parameter declaration. But when you invoke the method, and you have to pass an argument to it, you can use:
myMethod( new SuperClass() );
In the same way that you didn't need to give a name to your 15.7
before, you don't need to give a name to your new object now. When Java passes it to the method, the method sees it as the value of myParameter
.
Your second misconception is about names of objects. Objects don't actually have names. But they do have references. You can refer to an object from a local variable, from a field or from inside another object. In those cases, you give the reference a name, not the object. So you can do something like this:
Superclass myVar = new SuperClass();
Superclass anotherVar = myVar;
What you have here is two reference variables. You assign a reference to a new object to myVar
. And then you assign a reference to the same object to anotherVar
. Both myVar
and anotherVar
refer to the same object. The object does not have a name. You can now do something like myVar = null
. But the object will still exist, and you'll be able to access it through anotherVar
.
Think of reference variables as arrows. You give the arrow a name, and you can point the arrow at any object of the appropriate type. Or you can assign null
to it which means the arrow is not pointing at anything.
Back to the issue of parameters, sometimes you can see something like this:
myMethod( new SuperClass() {
// Code here
} );
This is exactly the same as we did before. It's a method invocation. The code you see in the braces is not the code for myMethod
. It is in fact the code for the new object. It's an anonymous class, which extends SuperClass
, and has some of its own code in those braces. So the class is defined and an instance is created of it, and that instance - of an anonymous class extending SuperClass
is what's being passed as an argument to myMethod
. Inside myMethod
, it can be accessed with myParameter
. It's the same as writing this somewhere in the same file:
private class SomeClass extends SuperClass() {
// Code here
}
And then calling `myMethod` like this:
myMethod( new SomeClass() );