0
What am I doing wrong here?
I've become somewhat familiar with the code, but when I introduce arrays, it falls apart. class Primary{ abstract class Job { int[ ] stats; String name; } class Tank extends Job { Tank Paladin = new Tank() { int[ ] stats = {4, 9, 10, 4, 4, 5, 4, 7, 1, 3}; String name = ("Pld"); } } public static void main(String[ ]args){ System.out.println(stats[2]); } }
4 Answers
0
Well you're missing out the semi-colon in your constructor
String name = ("Pld");
}; //It should be here
You forgot to put the main method inside the Tank class, you're trying to call a non-static variable (stats) inside a static scope (main), lastly the NPP exception because the compiler cannot see any initialization for the array stats. Since arrays are OBJECTS.
Try this code:
abstract class Job {
static int[] stats={4, 9, 10, 4, 4, 5, 4, 7, 1, 3};
String name;
}
public class Tank extends Job {
public String Tank() {
String name = ("Pld");
return name;
}
public static void main(String[ ]args){
Tank Paladin=new Tank();
System.out.println(stats[2]);
}
}
0
That works, thank you. But if I want several different jobs, like paladin, wizard and cleric, each with different 'stats', how do i achieve this? paladin.stats [] doesnt seem to work. I want an undefined int [] stats; declared in job, and then assign the stats, or array values, later depending on paladin or wizard or whatever. I feel like there is an easier way than just stats [0] = 3;
stats [1] = 6;
and so on...
0
public class Job {
int str;
int[ ] stats = new int[2];
void motto() {
System.out.println("I stand ready!");
}
}
class MyClass {
public static void main(String[ ] args) {
Job paladin = new Job();
paladin.str = 4;
paladin.stats[0] = 4;
paladin.stats[1] = 9;
Job gladiator = new Job();
gladiator.str = 10;
System.out.println(paladin.stats[1]);
}
}
With this code, it works. But shouldn't I be able to assign values to the array as a whole, rather than paladin.stats[0] = 4;
i should be able to do paladin.stats [] = {4,10}
0
basically i want to assign values to an array, not a value to an array index