0

I've one doubt about java reference type creation.

Suppose I've one class below

public class DefaultRepositorySelector
  implements RepositorySelector
{
  final LoggerRepository repository;

  public DefaultRepositorySelector(LoggerRepository repository)
  {
    this.repository = repository;
  }

  public LoggerRepository getLoggerRepository()
  {
    return this.repository;
  }
}

And am calling above class's constructor DefaultRepositorySelector somewhere in another class, like below.

repositorySelector = new DefaultRepositorySelector(new NOPLoggerRepository());

As you can see am Initializing a class new DefaultRepositorySelector(new NOPLoggerRepository()) and constructor accepts NOPLoggerRepository instance which is having an implementation of LoggerRepository Interface.

My doubt here is, we are directly passing a new NOPLoggerRepository() as a parameter in constructor which is an instance not a reference type, But constructor is holding a reference type LoggerRepository.

Am not able to understands the flow here, because according to flow when we are creating an instance we passing new object but not a reference to that object but in class's definition constructor accepts reference type of that object.

So at run time, How its taken care when we directly pass an instance but method or constructor accepts reference type of that instance ? Who gets created first reference type of OR Instance ? I think reference type but am not sure how its working behind the scenes..!

My question sounds very silly but please help to understand this ..!

Thanks

Lenny
  • 63
  • 1
  • 9

4 Answers4

1

An object of class NOPLoggerRepository is created and passed to the method. That's valid because NOPLoggerRepository implements LoggerRepository.

Rules about when an object is no longer reachable (and available for garbage collection) mean it won't be collected before being passed in (in case you were worrying about such weirdness).

When the constructor has completed the DefaultRespositorySelector holds a reference to the object passed in. However we know (in this case) it's underlying class is NOPLoggerRepository.

All good. The compiler knows when the underlying type may not be the explicit type of a reference and by various mechanisms ensures the correct behaviour. (Glossing over a whole load of stuff some implementation defined).

In java you never 'really' pass a object and class variables don't hold 'objects'. They are always references to objects.

In casual conversation we all often say things like "I'm passing an NOPLoggerRespository to the constructor" but we really mean I'm passing a reference to a NOPLoggerRespository to the constructor. That's OK because it's always a reference so long as we all understand it's a fine shorthand.

Persixty
  • 6,693
  • 2
  • 10
  • 31
0

I'm not quite sure I correctly understand your question.

You are going to create the object with the name repositorySelector of type DefaultRepositorySelector. While calling the constructor you need to have a reference of a LoggerRepository, which is a reference to a existing object (or null, if you pass null).

So, to get a reference, you pass new NOPLoggerRepository().

The expressions in parenthesis are evaluated first, so before the constructor of DefaultRepositorySelector can be called, the NOPLoggerRepository will be created as an anonymous object. That is, the object is created and is given no name. But we have the reference to it, which means we know where it is located on the heap.

As NOPLoggerRepository is of type LoggerRepository the constructor of DefaultRepositorySelector accepts the reference - the location of the anonymous object located on the heap and the object, which is named repositorySelector will be created.

Long story short, before calling a method (or constructor) the parameters are evaluated first (object creation, mathematical calculations, other method called and evaluated etc.), then the actual method can be called with the evaluated arguments.

Jochen
  • 173
  • 1
  • 8
0

The general idea of

Java is pass by Value

in objects does pretty much varie from premitive types .

in your code :

repositorySelector = new DefaultRepositorySelector(new NOPLoggerRepository());

the (new NOPLoggerRepository() is considered an Ignored Instance for the code scope in wich is invoked . the thing in here is when you invoked the new DefaultRepositorySelector it did nothing at first as the parameters are invoked first , so in the ordered sequence

  1. new NOPLoggerRepository() is invoked and created a new instance with no refrence on it on the heap .
  2. new DefaultRepositorySelector is invoked and do ask for it's RepositorySelector instance wich is located with no any refrence in the example .
  3. the JVM create the sole refrence for that object in your constructor and then another one (when your constructer pops off) in your class a .

so in short . it is still by refrence but just behind the scene .

-1

What you're referring to is commonly known as dependency injection. First, the object that's being injected is instantiated, and then the dependent object. SO answer here.

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Ivan Kukic
  • 425
  • 4
  • 13
  • Nope. No DI here. The object is passed as parameter to the constructor and thus created before! – Gyro Gearless Feb 19 '19 at 16:28
  • @GyroGearless I very much disagree, this is a textbook example of DI. Again, you can read the post I linked, or explain why this is not dependency injection. – Ivan Kukic Feb 19 '19 at 16:32