0

I'm trying to wrap my head around the correct naming of objects. So let's say I call Fruit apple = new Fruit(apple); So is Fruit (1st) the datatype apple (1st) the name of the object Fruit() a call to the constructor apple (2nd) a reference to an object named apple Or am I completely off on the naming? I know how they function, but I just want to get the naming right.

2 Answers2

2
Fruit apple = new Fruit(apple);

First, this wouldn't compile. You're trying to use apple before it's initialized.

Let's instead take something like

Fruit apple = new Fruit(numOfSeeds);

The left hand side of the expression (to the left of the = assignment operator) declares a new variable of type Fruit.

The right hand side of the expression is a new instance creation expression of type Fruit. This new instance creation expression invokes a Fruit constructor with a single parameter. The expression passes in a single argument to this constructor invocation. This argument will be the value resolved from evaluating the variable numOfSeeds.

When the expression on the right hand side is evaluated, the JVM will create a new instance of type Fruit and assign the value of the reference to that instance to the variable on the left hand side.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

So is Fruit (1st) the datatype apple (1st) the name of the object Fruit() a call to the constructor apple (2nd) a reference to an object named apple

Almost correct. The first Fruit is not only a datatype, but a class name as well, namely, the class to be instantiated. Also, as Sotirios Delimanolis pointed out, your example would not compile, because in it you would try to declare an object and use it in a constructor in the same time.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175