-4

I'm trying to learn Java, I sort of know Javascript but I was wondering how I could do this in Java

I use this for Javascript.

var player = {
X: 32,
Y: 32,
Cloned: false;

};

I would use this to drawImage(player.x, player.y) For example, how would I do something like this in java?

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315

3 Answers3

2

In Java, which is statically typed, there is no direct equivalent of the JavaScript ad-hoc object presented1.

Instead, one would create a [Player] class representing the "object" and then instantiate and use one or more instances. This class/instance approach, along with subtyping, is the core of the Java OOP model; there is no escape or viable alternative in Java - when using Java, write Java.

public class Player {
    protected int x;
    protected int y;
    // and maybe `cloned`, etc.

    // maybe an "action"
    public void move(int x, int y) {
        this.x = x;
        this.y = y;
    }

    // maybe a getter/setter
    // (although `move` above may obsolete the use of such setter)
    public int getX () { return x; }
    public void setX (int x) { this.x = x; }

    // and other relevant methods ..
};

And usage later might look like

// create new instance of class/type
Player player = new Player();
// manipulate and use
player.move(19, 81);
drawImage(player.getX(), player.getY());

While there is a "setter" provided in the Player class, only the "getter" was required for the usage; I prefer to use actions (e.g. move), and reducing mutable state when applicable, instead of allowing direct field manipulation as this can increase encapsulation. In any case, see How do getters and setters work? wrt this tangent.

Also consider "programming to interfaces"; for a real implementation I would likely have an Entity interface, implemented by the Player class, which understands positional movements and location. (While this is even more tangential, I wish that I had started programming "OOP" in this manner and I try to eschew type contracts based on implementing class or subtype relations!)


1 This is true of Java, but it is not inherent of static typing; for instance, C# has anonymous types (albeit the static typing and inference is limited within the function) and different languages support strongly-typed records and/or structural typing and/or dynamic typing in an otherwise statically-typed language. Java, however, requires that such is codified into a class for support at the language level.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • Getters/Setters are an anti-pattern. I wouldn't add them unless it's neccesary. – Carlos Vergara Jun 18 '14 at 22:26
  • @user1231958 It depends on whom is asked and where such getters/setters are used.. I would agree that the Java approach is generally *ugly* compared to languages that "naturally" support such idioms; but in any case, if the state is to be modified directly (e.g. not through an direct action such as `move`), then I would much rather see the use of a setter than direct field access. – user2864740 Jun 18 '14 at 22:28
  • That's true, in any case you should never expose directly the properties and even getter/setter are better than public properties. – Carlos Vergara Jun 18 '14 at 22:30
  • @user1231958 I usually trust the package-default, but I've just updated the post to explicitly make the fields protected to address that concern. – user2864740 Jun 18 '14 at 22:32
1

You're mixing two different things here. Javascript is a client-side language for website scripting, and Java is a generic OO language that's useful for several projects and it works server-side.

Now you want to define this in Java. You do this using a class:

public class Player {
    private int x;
    private int y;
    private boolean cloned;

    public Player (int x, int y, boolean cloned)
    {
        this.x = x;
        this.y = y;
        this.cloned = cloned;
    }

    public void move (int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public Point getCoordinates()
    {
        return new Point(x, y);
    }
}

And then you instantiate as new Player(32, 32, false);.

This is a very rough example and can indeed be improved but it's basically like this.

wingedsubmariner
  • 13,350
  • 1
  • 27
  • 52
Carlos Vergara
  • 3,592
  • 4
  • 31
  • 56
0

you can try this in Java using HashMap i will give you an example, it might not be the same but the logic is in it.

HashMap player = new HashMap();
player.put("X", "32");
player.put("Y", "32");
player.put("Cloned", "false");

System.out.print( player.get("X") ); // will print 32
System.out.print( player.get("X") ); // will print 32
System.out.print( player.get("Cloned") ); // will print false
wrecklez
  • 343
  • 2
  • 4