While Java has a type called object , this is not what you want. Almost everything in Java is an object, and there are two ways to handle this:
Define a strongly typed object with the correct properties as a class. Javascript has a similar concept, implemented in different ways, but it should be recognizable:
public class Car { private String type; private int model; private String color; public Car(final String type, final int model, final String color) { this.type = type; this.model = model; this.color = color; } public String getType() { return this.type; } public int getModel() { return this.model; } public String getColor() { return this.color; } } final Car car = new Car("Fiat", 500, "white");
Add methods to get and set attributes as needed, start, stop, disk, etc.
If you need a free collection of properties without behavior or restriction, use Map . Again, there is a Javascript equivalent (the {x: 1, y: 2} construct without using the new keyword).
final Map<String, Object> car = new HashMap<>(); car.put("type", "Fiat"); car.put("model", 500); car.put("color", "white");
The disadvantage of this approach is that the compiler cannot force the types of these objects (almost the same way), and the map cannot have custom behavior (in any reasonable way). In your example, model was a number, but that would allow you to assign something regardless of whether it makes sense (maybe someone is storing data on the server and using HttpConnection , all your code expecting the number to explode).
In Java, it is recommended that you use the first method if you know that you will have several cars, all with the same (or similar) properties. This allows the compiler to both provide and optimize the properties that, as you know, will exist, and inheritance allows you to add additional properties later. The class also allows you to define methods that work on this instance, which can help create an abstraction between parts of the system (you do not need to know how the machine starts, you just tell the machine to start it yourself).
For reference, Javascript equivalents:
//
What should stand out most obviously is that Java keeps track of the type of each variable. It is not visible that Java will not allow you to assign unknown properties, say car.mileage , where Javascript will happily add a new property. Finally, Java has the concept of visibility and makes things private (invisible to external viewers) by default. Replication of this in Javascript will look something like this:
var Car = function(type, model, color) { var _type = type; var _model = model; var _color = color; this.getType = function() { return _type; } this.getModel = function() { return _model; } this.getColor = function() { return _color; } } console.log(car.getColor());
In Javascript, you use closure to hide data. Java is hidden by default and requires you to provide data when you need it. This is an interesting choice, very important when comparing code bases, and can help keep classes independent of each other. It's also very easy (and tempting) to break when you start writing in OO languages, so something you need to keep in mind (using simple structures, will come back to haunt you).
ssube source share