Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8358957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:58:54+00:00 2026-06-09T10:58:54+00:00

From Effective Java ( Item 1 : Consider static factory methods instead of constructors

  • 0

From Effective Java (Item 1: Consider static factory methods instead of constructors):

The class of the object returned by a static factory method need not even exist
at the time the class containing the method is written. Such flexible static factory
methods form the basis of service provider frameworks, such as the Java Database
Connectivity API (JDBC). A service provider framework is a system in which
multiple service providers implement a service, and the system makes the implementations
available to its clients, decoupling them from the implementations.

I specifically do not understand why the book is saying that The class of the object returned by a static factory method need not even exist at the time the class containing the method is written ? Can some one explain using JDBC as the example .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:58:56+00:00Added an answer on June 9, 2026 at 10:58 am

    Consider something like the following:

    public interface MyService {
      void doSomething();
    }
    
    public class MyServiceFactory {
      public static MyService getService() {
        try {
            return (MyService) Class.forName(System.getProperty("MyServiceImplemetation"))
                    .getDeclaredConstructor()
                    .newInstance();
        } catch (InstantiationException |
                 IllegalAccessException |
                 InvocationTargetException |
                 NoSuchMethodException |
                 ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
      }
    }
    

    With this code, your library doesn’t need to know about the implementations of the service. Users of your library would have to set a system property containing the name of the implementation they want to use.

    This is what is meant by the sentence you don’t understand: the factory method will return an instance of some class (which name is stored in the system property "MyServiceImplementation"), but it has absolutely no idea what class it is. All it knows is that it implements MyService and that it must have a public, no-arg constructor (otherwise, the factory above will throw an Error).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following from Effective Java Item 11 (Override clone judiciously) where Josh Bloch
From Effective Java Second Edition, Item 28 : Do not use wildcard types as
From Effective Java 2nd edition item 67 page 266-268: The background thread calls s.removeObserver
Effective Java (Second Edition) , Item 4, discusses using private constructors to enforce noninstantiability.
This is a follow-up from this question : Effective Java 2nd Edition, Item 17:
From Effective Java 2ed Item 2: telescoping constructor pattern, in which you provide a
from p.46 Effective Java Joshua Bloch. Item 9: ALways override hashCode when you override
This is from Effective Java : // Implementing a fromString method on an enum
From Effective Java DOCUMENT ALL EXCEPTIONS THROWN BY EACH METHOD It should be noted
Following is from Effective Java: The association between a nonstatic member class instance and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.