0

I want to code a constructor for an array of size x with x being a parameter speciified in main().
My class:

public class CharA
{
  private char[] stack;
  private int n = 0;

  public void CharA (int max)
  {
    this.stack = new char[max];
    this.n = max;
  }

My main():

public class CharTest
{
  public static void main (String args)
  {
    CharA stack1 = new CharA(100);
  }
}

The error:

CharTest.java:5: cannot find symbol
symbol  : constructor CharA(int)
location: class CharA
    CharA stack1 = new CharA(100);
                   ^

There are several examples here where the same thing is done with an int array. Why doesn't it work for this char array?

3 Answers 3

6

remove void in your "constructor":

public CharA (int max) {
  // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Marv: It happens to the best :)
4

Replace public void CharA (int max) with public CharA (int max), because constructors don't have a return type.

Comments

2

The constructor method should not have a return type in its definition:

public CharA(int max) {...}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.