-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;
    }
}
7
  • 1
    Could you post your Card class? Commented Mar 6, 2013 at 4:24
  • 2
    Is your Card method constructor is Parameterized? Commented Mar 6, 2013 at 4:25
  • 1
    Is Card an enum by an chance? Commented Mar 6, 2013 at 4:25
  • 2
    this is due to You have a overloaded constructor in your class and you didn't write default constructor Commented Mar 6, 2013 at 4:25
  • 1
    Downvote for poor research effort. Please at least make an effort to google your question title before posting. Commented Mar 6, 2013 at 4:26

4 Answers 4

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();
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wow that was the problem...it was static. Thanks so much!
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(){}

Comments

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.

6 Comments

These are included by default. You don't need to add it to have it be an option.
when the user defines a parameterized constructor, the default constructor is not provided by the compiler.
@Legend What do you mean by "included by default"
@Legend JVM will ONLY add if there are no other Constructors defined. Try to test that yourself.
@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
|
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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.