Using variables in a nested JAVA class

I am very new to programming and wondering about using variables in what I think are 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 a general idea? I would like to keep variables that are "the same" but differ from class to class and are available differently, just like on a printed string. I am getting multiple errors, what should I do to keep the same concept and still keep it simple enough that I can understand it? Are there any easy-to-follow tutorials for people who are new to programming in general?

Thanks in advance!

+3


source to share


6 answers


The construction of this seems to be wrong.

What you are trying to use when working in an object oriented language is the basic model of what you want to represent.

These three static classes seem to represent the same type of object, so let's create a simple model for them. Think about models like cookie cutter. Each piece cut with this will have the same general "shape" but will have different characteristics (sprinkle, iron beard, etc.). This model should be in a 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 use it, you want to instantiate an 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 excellent resources for beginners already exist in the Java wiki tag ; give them a detailed reading. Try new things, and don't be afraid to ask (good) questions about things you don't understand.

+3


source


You have to create an inner class and then define instances of that class in 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;
    }

}

      

+2


source


You should use the concept of instances to differentiate people, not define a class for each person. You can define one class "Human" and instantiate James, Jackson, etc. To give them different attack / defense values, you can use constructors with arguments.

I feel you might find it helpful to read the introduction to Object Oriented Programming. Try searching for "object oriented programming".

+2


source


You can follow this path in two ways. You could subclass James, Janet, and Jackson to be of the same type by being BeginningGameTest. For example, James might be:

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

      

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

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

      

There are several concepts you should read:

  • Classes vs. Instances
  • Inheritance

And I also introduced you indirectly to the concept of setters (and getters) specific to Java beans.

+1


source


This will work:

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

      

Then this will work:

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

      

This is probably the best 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 the best design for realistic requirements: (allowing you to create players at runtime)

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

      

0


source


What you can simply do is create different objects of your class that will capture different variable values ​​and protect them. Here is the code for the same.

/ * some package; // don't include the 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();
    }
}

      

0


source







All Articles