1

I am trying to make a constructor for a text-based game that I am making for fun and cannot get the Character constructor to take a String and int. When used it requires only a char.

public class Character {

public int attack, rangedAttack, manaAttack, defense, rangedDefense, manaDefense, strength, agility, intelligence ,race;
String name;

public Character(String Name, int Race) {
    name = Name;
    race = Race; 
};

This is where I try to use the constructor.

public class QuestOfVallock{

public static void main(String[] args){
       Character self = new Character();
}
A--C
  • 36,351
  • 10
  • 106
  • 92

6 Answers6

3

You defined a constructor that takes a String and an int as a parameter, you have to call it with this way:

Character self = new Character("Bobby the mighty elf", 1);

You can not call the constructor without parameters if you don't define one, except if you don't define another constructor in the class.

Also:

  • Don't define a class called Character because it's already the name of an important class in the package java.lang.
  • Use some better naming convention for variables (either parameters or member variables): they should not be capitalized, but use camelCase.
  • It's better to encapsulate your variables by making them private and use getter/setter to access them. If they are constant (will not change during the object lifetime), you may consider making them "public final" or define only the getter, not the setter.
Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
2

When you define your own constructor with parameters like this

public Character(String Name, int Race) {
    name = Name;
    race = Race; 
}

Java will not a put a default constructor for you and it is undefined. You need to implement it on your own.

Edit 1:

Variable naming even parameters in Java should be in camel case

Edit 2:

Sometimes we define a setter methods or constructors which their parameter names (variable names) are also same on instance variables. Take a look for an example

public class Person
{
   private String name; // <-- 

   public Person(String name) // <--
   {

   }

   public void setName(String name)
   {

   }
}

When you do an assignment like this

public Person(String name)
{
   name = name;  
}

Well that's confusing. Java will give you a warning that there's no effect in variable assignment also we might think that name (instance) was assigned a new value.

To solve the problem use this.<variable name>

public Person(String name)
{
   this.name = name;  
}
Glenn
  • 12,741
  • 6
  • 47
  • 48
2

It looks like a namespace collision with java.lang.Character.

Try declaring a package to avoid ambiguity or rename the Character class to something else.

package mygame;
public class Character {

    public int attack, rangedAttack, manaAttack, defense, rangedDefense, manaDefense, strength, agility, intelligence ,race;
    String name;

    public Character(String name, int race) {
        this.name = name;
        this.race = race; 
    }
}

Then instantiate your Character class like this:

mygame.Character self = new mygame.Character("John Doe", 1);

Update: As others have pointed out, Java does not create a default constructer (one with no parameters) for you if you have one or more constructors defined. But I still stand by my answer that packages are the way to go. The use of the default package is discouraged and only exists for small applications beginning development. See Is the use of Java's default package a bad practice?

Community
  • 1
  • 1
styfle
  • 22,361
  • 27
  • 86
  • 128
  • Wrong, bro!! Constructors do not end with semi colon `;`. Plus you are missing the closing brace for the class `}` :) – An SO User Feb 15 '13 at 06:18
  • @LittleChild You can tell I didn't compile my code. I just wrote my answer straight into the text box. I fixed the syntax and added another paragraph at the bottom. I hope it is satisfactory. – styfle Feb 15 '13 at 06:31
  • Neither did I compile the code but ... never mind. No offense intended :) – An SO User Feb 15 '13 at 06:33
1

Also note that...

public class Character {

    public int attack, rangedAttack, manaAttack, defense, rangedDefense, manaDefense, strength, agility, intelligence ,race;
    String name;

    public Character(String Name, int Race) {
        name = Name;
        race = Race; 
    } //<-- shouldn't have semicolon here...
} //<-- should match the braces for the class...
chuthan20
  • 5,389
  • 1
  • 17
  • 21
0

When you define a constructor with parameters, then the default constructor is not available any more, until you explicitly declare it:

So you should add this constructor:

public Character(){ /* do something */ }

or call the correct constructor. Remember Java has no default value for arguments.

Tom
  • 43,810
  • 29
  • 138
  • 169
0

First
Avoid the name Character because there is a class in java.lang package that is named Character which is a wrapper class for the primitive data type char.
The java.lang.Character class is used for auto-boxing conversions, more info here

Second
Rename that class to Champion and it will definitely work. :)

Third
you are not passing proper parameters to the arguments. Your constructor needs parameters while you left the parantheses blank

What it looks like

public class Character {//<-- avoid this name

public int attack, rangedAttack, manaAttack, defense, rangedDefense, manaDefense, strength, agility, intelligence ,race;
String name;

public Character(String Name, int Race) {
    name = Name;
    race = Race; 
};//<-- Y U PUT SEMICOLON ?  

public class QuestOfVallock{

public static void main(String[] args){
       Character self = new Character(); //<-- Y U NO GIVE HIM PARAMETERS ?
}    

What it should be

public class Champion { //<-- Java-approved name

public int attack, rangedAttack, manaAttack, defense, rangedDefense, manaDefense, strength, agility, intelligence ,race;
String name;

public Character(String Name, int Race) {
    name = Name;
    race = Race; 
}//<-- No semicolon
}//<-- Closing brace for class

public class QuestOfVallock{

public static void main(String[] args){
       Champion self = new Champion("Little Java Child",1001); //<-- Unique race
}
}//<-- closing brace for class



What I also suggest is making your Race some constants in an interface
public interface RaceConstants {
    int ELF = 1;
    int JAVA_CHILD = 1001;
    int DWARF = 2;
}  

so your main() looks like this:

public static void main(String[] args){
       Champion self = new Champion("Little Java Child",RaceConstants.JAVA_CHILD; //<-- Unique race
}
An SO User
  • 24,612
  • 35
  • 133
  • 221