Reputation: 13
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();
}
Upvotes: 1
Views: 539
Reputation: 24610
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?
Upvotes: 2
Reputation: 12809
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;
}
Upvotes: 2
Reputation: 24998
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
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 ?
}
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
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
}
Upvotes: 0
Reputation: 5409
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...
Upvotes: 1
Reputation: 15523
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:
Character
because it's already the name of an important class in the package java.lang.Upvotes: 3
Reputation: 45114
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.
Upvotes: 0