A Library and a Framework are both built from sets of related reusable components. Generally speaking, a Framework is an encompassing set of classes that is designed to call your classes and methods. Not the other way around. So, typically, a Framework contains the main() method. A Library, on the other hand, is a set of related components that your application will call and make use of. In that case it's your app's code that is responsible for providing a main() method. Reusable components can be any class that encapsulates the data and behavior in a way that it can be used in one or more places in an application. Reusable components make things clearer and less redundant and are often more robust because they can be tested independent of the application.
This is a classic question, but I often find it helps to think about practical examples.
A framework would be something like Java Enterprise Edition, or Spring, where pretty much all your code is tightly coupled to the framework, because you are coding to the framework's requirements and it calls your code (as Campbell points out). You cannot easily swap one framework for another e.g. from JEE to Spring - without a lot of re-writing. And often you can't mix two different frameworks together without a world of pain.
A library is something that you call from your code e.g. a Maths or DateTime library or a JSON parser etc, where it's easy to swap one for another, and all you have to change is the specific call to the library function because the library does not affect the rest of your code. You can also mix and match different libraries for different things.
I think JEE and Spring have both evolved over the years to allow greater flexibility in how you use their frameworks (indeed, Spring was invented as a deliberate effort to avoid the complexity and tight coupling of J2EE), but you still end up with a lot of coupling between your application and the framework, whichever one you use.
Meanwhile, web development seems to be full of frameworks too, which seems like a nice money-making exercise for some folks...
In short as a general rule, you might define one or more re-usable Components in a Library. A Framework might contain multiple libraries.
A Framework is a collection of resources working towards a common goal. For example, Spring provides a common core that creates and manages JavaBeans. It has Modules, which are collections of libraries intended for a specific abstract purpose, such as Spring Data for persistent storage, Spring Security, Spring Scheduler and the like. Then within those logical modules are physical modules like Spring JDBC, Spring JPA and so forth, each of which comes with its own set of libraries. Spring piles on the layers pretty deep as you can see.
"privilege" comes from the Latin words for "private" and "law" (legal) and dates to feudal times. To "claim privilege" meant that you were above the laws that applied to the common people.
posted 2 days ago
Tim Holloway wrote:In short as a general rule, you might define one or more re-usable Components in a Library.
Does that mean that if library will be consisted of a number of jars, then some of these jars may constitute a reusable component ? What exactly differentiates a library from reusable component?