use of pre-instanciated object in lib or not


I noticed that most arduino libs define a class and also instanciate an object of this class:
class LibClass {. };
extern LibClass Lib;
LibClass Lib;

NTPClient, TwoWire, ...

Question 1: I understand this is done as a way to simplify the declaration (user can use the pre-instanciated object), and possibly to share one global variable potentially used by other libraries ?

is this correct ?

Question 2: can it be a problem if I instanciate another object, instead of using the one from lib ?

Question 3: if I instanciate and use another object, will the compiler remove the memory used by the other instanciated object from library ?

I did not easily find information on those topics, but I appreciate any pointer.

is this correct ?

It's all about making the libraries more beginner friendly. A beginner finds that instantiation code very confusing. We often get posts here on the forum from people who did a copy past of a fragment of a program using an object from a library without also adding the instantiation code and then can't figure out why they get a "was not declared in this scope" error.

It makes sense to do this in the cases where it's likely the user will only need a single instance. In other cases (e.g., Servo) the user is likely to need multiple instances. In this case, you are likely to find that the global object approach is not done.

Sometime there are reasons for there to only ever be one instance of an object class. This is called a "singleton" and, if the class is written correctly, there is no way to create a second object of that class.

It makes sense for the library to create an instance only if there are no arguments to the constructor.

You could always just use a #define if you want to use a different name for the pre-instantiated object:

#define MyInstanceName Lib