Using variables in a nested class JAVA

I am very new to programming and have a question about using variables in what I believe to be called "nested classes."

class BeginningGameTest {

    int attack;
    int defend;

    public static class James
    {
        attack = 25;
        defend = 15;
    }

    public static class Janet
    {
        attack = 45;
        defend = 1;
    }
    public static class Jackson
    {
        attack = 10;
        defend = 20;
    }


    public static void main(String[] args) {

        System.out.prinln(James.attack);

    }
}

Do I have the general idea down? I would like to save variables that are the "same" thing, but are different from class to class and are accessed differently like in the print line. I do get a few errors, what should I do to keep the same concept and still keep it fairly simple so I could understand it? Are there any easy to understand tutorials for people who are new to programming in general?

Thanks in advance!

Answers


The design of this seems incorrect.

What you're trying to go for when working in an object-oriented language is the basic model of something you wish to represent.

Those three static classes seem to represent the same type of object, so let's create a simple model for them. Think of models like a cookie-cutter. Every cookie cut with this will be the same generic "shape", but will have different characteristics about it (sprinkles, frosting beard, etc). This model should be in its own separate file.

public class Player {

    private String name;
    private int attack;
    private int defense;

    public Player(String theirName, int theirAttack, int theirDefense) {
        name = theirName;
        attack = theirAttack;
        defense = theirDefense;
    }

    // create getters and setters for their attack and defense
}

To actually make use of it, you'd want to instantiate the object.

public class BeginningGameTest {
    public static void main(String[] args) {
        Player p1 = new Player("James", 25, 15);
        Player p2 = new Player("Janet", 45, 1);
        Player p3 = new Player("Jackson", 10, 20);
        // interactions with the objects below
    }
}

Some superb beginner resources already exist in the Java tag wiki; give those a thorough reading. Try new things out, and don't be afraid to ask (good) questions about things you don't understand.


You should create an inner class then define instances of that class within the main method.

public class BeginningGameTest {


    public static void main(String[] args) {
        Player james = new Player(25,15);
        Player janet = new Player(45,1);
        Player jackson = new Player(10,20);

        System.out.println(james.getAttack());
    }
}

class Player{
    int attack;
    int defend;

    public Player(int attack, int defend){
        this.attack = attack;
        this.defend = defend;
    }

    public int getAttack() {
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

    public int getDefend() {
        return defend;
    }

    public void setDefend(int defend) {
        this.defend = defend;
    }

}

You should use the concept of instances to distinguish persons, rather than defining a class for each person. You can define a single class "Person" and instantiate James, Jackson etc. To give them each different attack/defence values, you can use constructors with arguments.

I feel that you might benefit from reading an introduction to object oriented programming. Try searching for "object oriented programming".


You can go two ways about this. You could create subclasses such that James, Janet and Jackson are all classes of the same type, being BeginningGameTest. For example, James could be:

public class James extends BeginningGameTest
{
    public James()
    {
        attack = 25;
        defend = 15;
    }
}

What I think you want James, Janet and Jackson to be, are not subclasses, but rather instances of the same class BeginningGameTest, like this:

BeginningGameTest James = new BeginningGameTest();
James.setAttack(25);
James.setDefend(15);

There are a few concepts you should read upon:

  1. Classes vs instances
  2. Inheritance

And I also implicitly introduced you to the concept of setters (and getters), typical for Java beans.


This will work:

public static class James
{
    static int attack = 25;
    static int defend = 15;
}
// ...

Then this would work:

public static void main(String[] args)
{
    System.out.prinln(James.attack);
}

This is probably a better design:

public class Player()
{
   public static enum NAME { JAMES, JANET };

   int attack, defend;
   public Player(NAME name)
   {
      switch (name)
      {
         case JAMES:
            attack = 25;
            defend = 15;
            break;
         // ...
      }
   }

   public static void main(String[] args) throws Exception
   {
      System.out.println(new Player(NAME.JAMES).attack);
   }
}

This is a better design for realistic requirements: (allowing run-time creation of players)

int attack, defend;
String name;
public Player(int attack1, int defend1, String name1)
{
   attack = attack1;
   defend = defend1;
   name = name1;
}

What you can simply do is create different objects of your class that will hold different values of variables attack and defend. Here is the code for the same.

/* package whatever; // don't place package name! */

class Main
{
    int attack,defend;
    public Main(int attack,int defend)
    {
        this.attack=attack;
        this.defend=defend;
    }
    public void show()
    {
        System.out.println("attack: "
        +attack+" defend: "+defend);
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        Ideone James = new Main(125,15);
        James.show();
        Ideone Janet = new Main(45,1);
        Janet.show();
        Ideone Jackson = new Main(10,20);
        Jackson.show();
    }
}

Need Your Help

Spell Checker in Nutch 1.0

java nutch

Can anyone tell me how to implement spell checker in nutch 1.0?