Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create objects from a class with private constructor?

Tags:

java

oop

I have a class Game that is my main class and a second class Card. Class Card hast its properties and constructor private, only function init is public. Function init checks values for plausibility and if everything is fine than the constructor gets the values and creates an object. Now I wannt in class Game to create an object from class Card. How should I do this?

Here is my code:

Class Game:

import java.util.List;
import java.util.Vector;


public class Game {


public  static void main(String[] args)
{
   /*
CREATING NEW CARD OBJECT
 */

    int value = 13;
    Vector<Card> _card_set = new Vector<Card>();
    for (int i = 2; i < 54; i++)
    {

        if(--value == 0)
        {
            value = 13;
        }

        Card _myCard;
        _myCard.init(i,value);
     }
   }
 }

Class Card:

public class Card {

private int index;
private  int value;
private String symbol;

/*
CREATING A PLAYCARD
*/

private Card(int index,int value)
{
    this.index = index;
    this.value = value;
    value = (int) Math.floor(index % 13);

    if(this.index >= 2 && this.index <= 14)
    {
        this.symbol = "KARO";
    }
    else if (this.index >= 15 && this.index <= 27)
    {
        this.symbol = "HERZ";
    }
    else if (this.index >= 26 && this.index <= 40)
    {
        this.symbol = "PIK";
    }
    else if (this.index >= 41 && this.index <= 53)
    {

        this.symbol = "KREUZ";
    }
    System.out.println("Card object wurde erstellt: " + symbol + value);
    System.out.println("------<><><><>------");
}

/*
SHOW FUNCTION
GET DETAILS ABOUT PLAYCARD
 */
public String toString()
{
    return "[Card: index=" + index + ", symbol=" + symbol + ", value=" + value + "]";
}

/*
Initialize Card object
 */

public Card init(int index, int value)
{
    /*
    Check for plausibility
    if correct constructor is called
     */

    if((index > 1 || index > 54) && (value > 0 || value < 14))
    {
       Card myCard = new Card(index,value);
        return  myCard;
    }
    else
    {
        return null;
    }
  }
}
like image 541
CMS Avatar asked May 26 '14 13:05

CMS


People also ask

Can we create object of a class with private constructor?

A private constructor does not allow to create an object outside the class.

Can I create object of private class?

yes. private is an access modifier, as you might have learned that restricts member to be access just within declaring scope. So as a member of another class , private class can be accessed in that class only.

How do I create an instance of a private constructor?

Using Private Constructors in the Singleton Pattern We can create an instance by calling SingletonClass. getInstance() – this either returns an existing instance or creates one if this is the first instantiation. We can only instantiate this class by using the getInstance() static method.

How do you create an instance of a class having a private constructor in C#?

class NLog { // Private Constructor: private NLog() { } public static double e = Math. E; //2.71828... } The declaration of the empty constructor prevents the automatic generation of a parameterless constructor. Note that if you do not use an access modifier with the constructor it will still be private by default.


2 Answers

You should define your init method as static, implementing the static factory method that Braj talks about. This way, you create new cards like this:

Card c1 = Card.init(...);
Card c2 = Card.init(...);
...
like image 119
Jorge_B Avatar answered Sep 30 '22 14:09

Jorge_B


As @Braj mentioned in comments, you can use static factory. Private constructor cannot be accessed outside of class, but it can be accessed from inside, like the following:

public class Test
{

    private Test(){

    }

    static Test getInstance(){
        return new Test();
    }
}

This pattern can be used for making Builders, for example.

like image 37
nikis Avatar answered Sep 30 '22 13:09

nikis