initialize object directly in java

前端 未结 10 994
逝去的感伤
逝去的感伤 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 05:11

    It is possible with the keyword new and using constructors, but not like the String, that is a very special kind of object.

    0 讨论(0)
  • 2020-12-08 05:11
        package com.company;
    
    public class InitializationOfObject {
        int a ;
        int b ;
    
        InitializationOfObject(){
        }
    
        InitializationOfObject( int r , int n){
            this.a = r;
            this.b = n;
            System.out.println("Object initialization by constructor  ");
        }
    
        void methodInitialization(int k, int m){
            System.out.println("object initialization via method");
            this.a = k;
            this.b = m;
        }
    
        void display(){
            System.out.println("k = " +a+ "m = "+b);
        }
    
        public static void main(String... arg){
            InitializationOfObject io = new InitializationOfObject();
            InitializationOfObject io2 = new InitializationOfObject(45,65);
            io.a = io2.a;
            io.b = io2.b;
            io.display();
    
            io.methodInitialization(34,56);
            io.display();
    
            io.a = 12;
            io.b = 24;
            System.out.println("object initialization via refrence");
            System.out.println("a = "+io.a+" "+ " b ="+io.b);
        }
    
    }
    

    //Object initializatian by construtor

    k = 45m = 65
    

    object initializaion via method

    k = 34m = 56
    

    object initialization via reference

    a = 12  b =24
    
    0 讨论(0)
  • 2020-12-08 05:16

    Normally, you would use a constructor, but you don't have to!

    Here's the constructor version:

    public class MyData {
        private String name;
        private int age;
    
        public MyData(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        // getter/setter methods for your fields
    }
    

    which is used like this:

    MyData myData = new MyData("foo", 10);
    


    However, if your fields are protected or public, as in your example, you can do it without defining a constructor. This is the closest way in java to what you want:

    // Adding special code for pedants showing the class without a constuctor
    public class MyData {
        public String name;
        public int age;
    }
    
    // this is an "anonymous class"
    MyData myData = new MyData() {
        {
            // this is an "initializer block", which executes on construction
            name = "foo";
            age = 10;
        }
    };
    

    Voila!

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

    You have to make a constructor method for the object, which takes in parameters of the fields you want values for.

    Example:

    public myClass( int age, String name)
    {
       this.age = age;
       this.name = name;
    }
    

    Then in the class you want this:

    myClass class = new myClass(24, "name");
    
    0 讨论(0)
提交回复
热议问题