initialize object directly in java

前端 未结 10 993
逝去的感伤
逝去的感伤 2020-12-08 04:26

Is that possible to initialize object directly as we can do with String class in java:

such as:

String str=\"something...\";

I wan

相关标签:
10条回答
  • 2020-12-08 04:58

    There are two types of Constructors in java.

    1. Default constructor
    2. Parameterized constructor

    You should create a parameterized constructor to create your object.

    0 讨论(0)
  • 2020-12-08 05:01
    class MyData{
    
        public MyData(String name, int age) {
            this.name = name;
            this.age = age;
        }
        public String name;
        public int age;
        }
    

    Then you can instantiate your class this way:

    MyData myData = new MyData("name", 24);
    
    0 讨论(0)
  • 2020-12-08 05:01

    There is no alternative to constructors (along with new operator) in java during the object initialization. You have mentioned as

    String str = "something"

    you can initialize string that way, because String is a literal in java. Only literals can initialized that way. A a composite object can not initialized, but only can be instantiated with the new operator with the constructors.

    0 讨论(0)
  • 2020-12-08 05:02

    If you have a class Person:

    public class Person {
    
        private String lastName;
        private String firstName;
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
    }
    

    You can actually create a new Person object and initialize its firstName and lastName with the following:

     Person person = new Person(){{
         setFirstName("My FirstName");
         setLastName("MyLastName");
     }}
    

    This is used quite often when defining Spring Configuration using Java code instead of XML configuration.

    0 讨论(0)
  • 2020-12-08 05:07

    I know that with constructors, but any alternative way is present or not?

    No, there are no alternatives to constructors.

    That's basically one of the fundamental guarantees of the language. An object can't be constructed by any other means than through its constructors and there's no alternative syntax then the usual new ConstructorName(...).

    The closest idea I can come up with would be to have a static factory method called say, mc:

    class MyClass {
        ...
        public static mc(String name, int age) {
            return new MyClass(name, age);
        }
    }
    

    and then do

    import static some.pkg.MyClass.mc;
    
    ...
    
    MyClass obj1 = mc("name",24);
    
    0 讨论(0)
  • 2020-12-08 05:07

    The following does what you want, but not in the way that you would expect.

    So in a class calling MyData, you would use

    Public MyData x = new MyData();
    @PostConstruct public void init() {
         x.setName("Fering");
         x.setAge(18);
    }
    

    So once the object is construsted, these commands are run, which allows you to populate the object before anything else runs.

    So with this you do not have to use anonymous subclasses, or create new constructors, you can just take the class and then use its functions, before anything else would.

    0 讨论(0)
提交回复
热议问题