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 8104467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:51:08+00:00 2026-06-05T23:51:08+00:00

public class A { private A(int param1, String param2) {} public static A createFromCursor(Cursor

  • 0
public class A {
    private A(int param1, String param2) {}

    public static A createFromCursor(Cursor c) {
        // calculate param1 and param2 from cursor
        return new A(param1, param2);
    }
}

Is there a design pattern for this kind of construction code? If so, what’s the purpose of this pattern? Why not just use:

// calculate param1 and param2 from cursor
new A(param1, param2);
  • 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-05T23:51:10+00:00Added an answer on June 5, 2026 at 11:51 pm

    So, to sum up.

    That pattern is called Static Factory Method and it’s described for example here: How to use "Static factory methods" instead of constructors?

    In simplest form it looks like this:

     class A {
    
         public static A newA() {
            return new A();
         }
         private A(){}
     }
    

    Your example is little bit more complex as it includes computing parameters for invoking constructor

    public class A {
        private A(int param1, String param2) {}
    
        public static A createFromCursor(Cursor c) {
            // calculate param1 and param2 from cursor
            return new A(param1, param2);
        }
    }
    

    The purpose of using such way for creating new object may be the need to repeat those calculating every time before calling new A(params) directly. So it’s just avoiding repeating yourself, and simplest way to achieve this is creating a method.

    Furthermore using the same way, you can provide even more options to create new A. For example you can change the way your calculation works with:

        public static A createFromCursorDifferently(Cursor c) {
            // calculate param1 and param2 from cursor in different way
            return new A(param1, param2);
        }
    

    Then you may pass the same parameter to this method, and result will be different (coz method name is varies).

    And of course you can create your new A from any other parameter, using the same constructor as before:

        public static A createFromObject(Object o) {
            // calculate param1 and param2 from object
            return new A(param1, param2);
        }
    

    So you have much more possibilities with Static Factory Methods than with simple usage of constructors only.

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

Sidebar

Related Questions

public class SelfCallingTest { private static int counter; public void SelfCallingMethod(int counter) { Console.WriteLine(The
public class Ex7 { private int fld; private void meth(int val) { fld =
public class Arrys { private int[] nums; //Step 3 public Arrys (int arrySize) {
Given class Value : public class Value { private int xVal1; private int xVal2;
Superclass source code public class Date { private int month; private int day; private
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
these are my entities (simplified): public class IdentificationRequest { private int id; private ICollection<IdentificationRequestStateHistoryItem>
The following code works fine. class A { private: int _value; public: class AProxy
The following code works & runs perfectly. public class Complex { private int real,
So, here's my problem. I have objects like this public class FooClass{ private int

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.