Java objects and classes used in another class

I am having trouble porting a Java project using Python. Basically what I don't understand is what line 4 does. Dice is a class defined elsewhere. I'm more than sure that it doesn't create an object or instance from the Dice class. Is this just memory allocation for later creation? I really have no idea.

Code in question:

1 public class Move {
2
3  // the dice used for this move
4  protected Dice dice;
5  // the set of movements used to perform this move, in this order
6  protected Movement[] movements;
7  .....}

      

PS: stackoverflow has such a great community! So far all my problems can be solved by checking here. But now I'm lost and had to finally create an account;)

+3


source to share


3 answers


This statement declares a member of the class. Class members are different for local objects / variables because they are accessible from the whole class. In this case, it defines a protected object of type Dice

named Dice

.

The operation does not actually create an object Dice

; he simply claims to exist. If you want to use it, you can create it in a method. For example:



public class Move {
    protected Dice dice;
    ...
    public void someMethod() {
        dice = new Dice();
    }
}

      

+4


source


Yes, it just allocates memory for the object Dice

as a protected property of your class Move

.



Edit: This is not memory allocation until the object is clearly assigned. Rather, it defines a relationship: an object Move

consists of a protected object Dice

.

+1


source


It protected Dice dice;

's like declaring a site owner. This is just a definition. You say, here's my class, and I'll put in perhaps an instance of Dice called "dice".

The same for protected Movement[] movements;

Under the hood, a variable containing a pointer is defined. This pointer will eventually hold the value (memory location) that contains the object instance (like a Dice object)

+1


source







All Articles