0

In Javascript one could create an object like:

       newdata: {
            zero_to_one: {self: 0, bulk: 0, norm: 0},
            one_to_2: {self: 0, bulk: 0, norm: 0},
            two_to_4: {self: 0, bulk: 0, norm: 0},
            over_four: {self: 0, bulk: 0, norm: 0},
        }

Modifying the data in javascript is simple by just calling this.zero_to_one.self =2

How can i achieve the same in java

030
  • 10,842
  • 12
  • 78
  • 123
Geoff
  • 6,277
  • 23
  • 87
  • 197
  • 3
    Create a Java POJO, then use the mutator methods or set fields directly. Have you tried anything? This is a pretty basic thing to do in Java. – Tim Biegeleisen Jan 27 '19 at 10:42
  • 1
    Please refer https://stackoverflow.com/questions/3527264/how-to-create-a-pojo – Chaitanya Jan 27 '19 at 10:44
  • 1
    Please start from reading some basic literature about Java syntax, that will initially boost you up and save you plenty of time. – Andrei Makarevich Jan 27 '19 at 10:51
  • 1
    Better start by learning some basic syntax as everyone suggested. It's not like transitioning between java and kotlin and not expected to be very intuitive. – shakhawat Jan 27 '19 at 10:59

2 Answers2

1

Declaration and creation of an object in Java

SHORT VERSION

The conversion of JS to Java would be as follows:

JS

newdata: {
            zero_to_one: {self: 0, bulk: 0, norm: 0},
            one_to_2: {self: 0, bulk: 0, norm: 0},
            two_to_4: {self: 0, bulk: 0, norm: 0},
            over_four: {self: 0, bulk: 0, norm: 0},
}

JAVA

// ZeroToOne.java
public class ZeroToOne {

    int self; // Type self
    int bulk; // Type bulk
    int norm; // Type norm

    /**
     * GETTERS AND SETTERS
     */

    public int getSelf() {
        return self;
    }

    public void setSelf(int self) {
        this.self = self;
    }

    public int getBulk() {
        return bulk;
    }

    public void setBulk(int bulk) {
        this.bulk = bulk;
    }

    public int getNorm() {
        return norm;
    }

    public void setNorm(int norm) {
        this.norm = norm;
    }

}

In the same way you will be able to do it with one_to_2,two_to_4 and over_four.

This is called a simple object creation and is what we call POJO in java.

ℹ️ More information:
Plain_old_Java_object

LARGE VERSION

Following the previous example:

   public class ZeroToOne {

    // Attributes of the ZeroToOne class
    private int self; // Type self
    private int bulk; // Type bulk
    private int norm; // Type norm

    // Methods of the ZeroToOne class

    /**
     * GETTERS AND SETTERS
     */
    public int getSelf() {
        return self;
    }

    public void setSelf(int s) {
        this.self = s;
    }

    public int getBulk() {
        return bulk;
    }

    public void setBulk(int b) {
        this.bulk = b;
    }

    public int getNorm() {
        return norm;
    }

    public void setNorm(int norm) {
        this.norm = norm;
    }
}

Note that, in the class body, between the {} keys have been defined:

  • Three attributes (also called private fields): self,bulk and norm.

  • Six public methods (public): getSelf, setSelf,getBulk, setBulk,getNorm and setNorm.

In such a way that all the objects that are created from the ZeroToOne class will have a self, bulk and norm that will be able to store different values, being able to be modified or consulted when their defined methods are invoked :

  • setSelf/setBulk/setNormallows you to ASSIGN set to self / bulk / norm (int) to an object of theZeroToOne class.

  • getSelf/getBulk/getNormallows you to CONSULT get the self / bulk / norm of an object of theZeroToOne class.

To declare and create an object of class ZeroToOne:

    ZeroToOne callZeroToOne; // Declaration of variable p1 of type Person
    ZeroToOne zOne = new ZeroToOne (); // Create an object of the Person class

In addition, the same can be indicated in a single line:

ZeroToOne callZeroToOne = new ZeroToOne();

Modify the value

And where to directly modify the value of self you will have to do it in the following way:

    ZeroToOne callZeroToOne; // Declaration of variable p1 of type Person
    ZeroToOne zOne = new ZeroToOne (); // Create an object of the Person class
    zOne.setSelf (2); // We modify the value
    System.out.println (zOne.getSelf ()); // Impression of the result

What we would get

> 2
Nicolás Alarcón Rapela
  • 2,714
  • 1
  • 18
  • 29
  • If easy instantiation is important, one could also add a constructor taking name and age as parameters. – Henry Jan 27 '19 at 10:48
1
newdata: {
            zero_to_one: {self: 0, bulk: 0, norm: 0},
            one_to_2: {self: 0, bulk: 0, norm: 0},
            two_to_4: {self: 0, bulk: 0, norm: 0},
            over_four: {self: 0, bulk: 0, norm: 0},
}

If you look at the structure , it is Object within a Object . So what you can do is to create 4 child object zero_to_one, one_to_2,two_to_4,over_four .

All four child object has the same three field self,bulk,norm. You can also fit all four into one if you want.

A simple Object creation , what we call POJO in java requires the following :

public class ZeroToOne{
        String self;
        String bulk;
        String norm;

        public String getSelf() {
            return self;
        }

        public void setSelf(String self) {
            this.self= self;
        }

        public String getBulk() {
            return bulk;
        }

        public void setBulk(String bulk) {
            this.bulk = bulk;
        }

        public String getNorm() {
            return norm;
        }

        public void setNorm(String norm) {
            this.norm = norm;
        }

    }

After that you can get and set value using those getter/setter method.

Shifat
  • 732
  • 1
  • 6
  • 20