-2

I'm writing a program and I want to access a method from a different class. I know you are supposed to create a new object for the method you want to use, but every time I do that, it states, "The constructor Card() is undefined". I don't want to access the constructor though, I want to access a different method in that class.

Here is the very beginning of my method I want to bring my "other" method in to. I tried creating a new object but it gives me an error on that part that's in between the *__*:

public void addCard(Card[][]card) {
    Card card1;
    card1 = *new Card();*

The other method is simply called getCard(int r, Suit s) (this is in a different class). There is also a constructor method in this class and that's what the error seems to be talking about..?

If you could help that would be Great, thanks!

Here's my Card class:

public class Card {
private static Card[][] card = new Card[4][13];
private int rank;
public enum Suit{hearts, diamonds, clubs, spades};
private Suit suit;

private Card(int r, Suit s){
    this.suit = s;
    this.rank = r;
}

public int getRank(){
    return rank;
}

public Suit getSuit(){
    return suit;
}

public static Card[][] getCard(int r, Suit s){
    if(card == null){
        return card;
    }
    else{
        card = new Card[4][13];
        return card;
    }
}

4 Answers4

4

You should instantiate a new object like that:

Card card = new Card();

On the other hand, if you have in your design to use methods of classes without instantiating objects of that class, then you are looking for the static keyword. static defined methods and variables are defined to be a property of the class rather than the object. So, you can use static methods without initializing the object.

public Class A{
  public static void methodName(){
    //you can call that methods from anywhere using A.methodName();
  }
}
dlock
  • 9,447
  • 9
  • 47
  • 67
2

The constructor is called automatically when you try to setup an instance of the class.

A default no-args constructor is automatically generated for you as long as you don't create another constructor. Once you do, you will need to specify the no-args constructor yourself, like below.

public Card(){}
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
1

when the user defines a parameterized constructor, the default constructor is not provided by the compiler, hence the user has to define a default constructor if he wishes to create objects by not giving parameters to the constructor.

public Card()
{
    //do something
}

looks like you have private constructor, change your constructor to the following

public Card(int r, Suit s)
{
    this.suit = s;
    this.rank = r;
}

Note: if you want to create Card objects in the following way:

Card card1;
card1 = new Card();

you can either define public no argument constructor(i.e public Card(){}) or nothing(in case of nothing : compiler will provide no argument constructor)

if you want to create Card objects in this fashion:

Card card1, card2;
card1 = new Card();
card2 =  new card(1,new Suit());

you have to include both the constructor as I have shown above.

codeMan
  • 5,730
  • 3
  • 27
  • 51
  • These are included by default. You don't need to add it to have it be an option. –  Mar 06 '13 at 04:25
  • when the user defines a parameterized constructor, the default constructor is not provided by the compiler. – codeMan Mar 06 '13 at 04:26
  • @Legend What do you mean by "included by default" – Jayamohan Mar 06 '13 at 04:26
  • The JVM adds them automatically, such as importing java.lang, adding Object as a super class if non is stated, etc. –  Mar 06 '13 at 04:27
  • 1
    @Legend JVM will ONLY add if there are no other Constructors defined. Try to test that yourself. – Jayamohan Mar 06 '13 at 04:30
  • 1
    @Legend after the creation of parameterized constructor, JVM hides the implementation of deafault constructor. After than you need to define a zero argument constructor if you need to create a non parameterized object – Freak Mar 06 '13 at 04:31
0

When you write a constructor with argument then JVM hides the default construtor (with no parameters).So now you need to define a constructor with no arguments.So now you need to define a constructor

public Card(){
}

OR create object with parameterized constructor new Card(params)

Freak
  • 6,786
  • 5
  • 36
  • 54