-4

I'm having difficulty understanding the concept of the new keyword. I know its used to instantiate objects; e.g. If I had a class called Superclass, I could create a object of that class by writing:

Superclass supeclassobject = new Superclass(); 

I understand that but what I dont understand is that this is also acceptable: E.g. if your were passing a Superclass object to a method which takes it as an argument, then the following would still work:

public void MethodTakingSuperClassObjectAsArugment (new Superclass()){
*CODE HERE*
}

I cant understand how that works. You haven't given a name to the object so how could you refer to it in the method? This makes sense to me:

Superclass sobject = new Superclass();
public void MethodTakingSuperClassObjectAsArugment (sobject){
    *CODE HERE*
    }
Tarikh Chouhan
  • 425
  • 3
  • 5
  • 15
  • Objects don't have names. Variables have names. This is no different to `int x = 4; int y = 3; methodThatTakesAnInt(x + y);`. – Oliver Charlesworth Jan 10 '15 at 13:10
  • possible duplicate of [How are Anonymous (inner) classes used in Java?](http://stackoverflow.com/questions/355167/how-are-anonymous-inner-classes-used-in-java) – Joe Jan 10 '15 at 13:11
  • 1
    possible duplicate of [Class definition inside method argument in Java?](http://stackoverflow.com/questions/5180222/class-definition-inside-method-argument-in-java) – David Brossard Jan 10 '15 at 13:11
  • @OliverCharlesworth Hello helloobj = new Hello(); Haven't i created a object of type Hello and gave it a name of helloobj? – Tarikh Chouhan Jan 10 '15 at 13:12
  • No, `helloobj` is a reference variable, that refers to the object. – Oliver Charlesworth Jan 10 '15 at 13:16

1 Answers1

0

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() ); 
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79