0

in C# you can create a instance of a class and set the values of variables at the same time:

public class Object
{
    public virtual long Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int Version { get; set; }
    public long ParentId { get; set; }
}

public class Start
{ 
     Object object= new Object()
     {
         Id = 1,
         Name = name,
         ParentId = parentId,
         Description = null,
         Version= 2
     };
}

Is this possible in Java aswell and how?

3 Answers 3

2

The standard way for setting values when creating an instance is to just have a constructor:

class ExampleObject {
    long id;
    String name;
    String description;
    int version;
    long parentId;

    public ExampleObject(final long id, final String name, final String description, final int version, final long parentId) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.version = version;
        this.parentId = parentId;
    }
}

And then call it like:

ExampleObject exampleObject = new ExampleObject(1, name, null, 2, parentId);

It is possible to use a similar syntax to what you have shown, but it has quite a few downsides which you should research about before using it (and you also cannot use variables with this):

ExampleObject exampleObject = new ExampleObject() {{
    id = 1;
    name = "";
    parentId = 2;
    description = null;
    version = 2;
}};


class ExampleObject {
    long id;
    String name;
    String description;
    int version;
    long parentId;
}

What this does is creates an anonymous class with a static initialiser block. A static initialiser block looks like:

class ExampleObject {
    long id;
    String name;
    String description;
    int version;
    long parentId;

    {
        id = 1;
        name = "";
        parentId = 2;
        description = null;
        version = 2;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can create a constructor that accepts values for all the fields. This way, you can create a new instance of that object and set the values you want at the same time:

public class MyClass {
  public long id;
  public String name;
  public String description;
  public int version;
  public long parentId;

  /** Constructor **/
  public MyClass(long id, String name, String description, int version, long parentId) {
    this.id = id;
    this.name = name;
    this.description = description;
    this.version = version;
    this.parentId = parentId;
  }
}

public static void main(String args[]) {
  MyClass myClass = new MyClass(1, "name", "description", 1, 1);
}

By the way, it's not recommended (although you can) to name a class Object in Java, since Java also has a class with that very same name, and all Java classes extend from it (can lead to confussion).

Comments

0
              public class Object
              {
                public long id;
                 public String name;
               public String description;
                public int version;
              public long parentId;

                  public Object(long Id,string Name,string Description,int                Version,long Parent_Id)
                {

                 this.Id =Id ;
                 this.Name =Name ;
                  this.Description =Description ;
                 this.Version =Version ;
                 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.