代码之家  ›  专栏  ›  技术社区  ›  Mike Deck

在Java中创建对象的方法有哪些不同?

  •  157
  • Mike Deck  · 技术社区  · 17 年前

    前几天和同事谈过这个。

    很明显,使用构造函数的方法是什么,但是还有其他的方法吗?

    22 回复  |  直到 17 年前
        1
  •  261
  •   Anil Chahal    10 年前

    new

     MyObject object = new MyObject();
    

    Class.forName()

    MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();
    

    clone()

    MyObject anotherObject = new MyObject();
    MyObject object = (MyObject) anotherObject.clone();
    

    object deserialization

    ObjectInputStream inStream = new ObjectInputStream(anInputStream );
    MyObject object = (MyObject) inStream.readObject();
    

    here

        2
  •  66
  •   Tom Hawtin - tackline    14 年前

    • Class.newInstance
    • Constructor.newInstance
    • Object.clone
    • new
    • String
    • ...
    • throw null; "".toCharArray()[0]
        3
  •  24
  •   Confusion    10 年前

        4
  •  13
  •   Thomas Lötzer    15 年前

    String.class.newInstance()

        5
  •  9
  •   Naresh Joshi    6 年前

    new

    Employee emp1 = new Employee();
    

    newInstance() Class

    Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee")
                                    .newInstance();
    

    Employee emp2 = Employee.class.newInstance();
    

    Constructor

    Constructor<Employee> constructor = Employee.class.getConstructor();
    Employee emp3 = constructor.newInstance();
    

    clone()

    Employee emp4 = (Employee) emp3.clone();
    

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
    Employee emp5 = (Employee) in.readObject();
    

    Different ways to create objects in Java with Example

        6
  •  8
  •   Bill the Lizard    17 年前
        7
  •  6
  •   Vincent Ramdhanie    15 年前

     Object myObj = Class.forName("your.cClass").newInstance();
    
        8
  •  6
  •   stacker    15 年前

        10
  •  6
  •   PaÅ­lo Ebermann    13 年前

     A[] array = new A[len];
    

     A[] array = new A[] { value0, value1, value2 };
    

     A[] array = (A[]) Array.newInstance(A.class, len);
    

        11
  •  5
  •   Peter Lawrey    14 年前

    • anewarray multianewarray newarray new
        12
  •  4
  •   John Meagher    17 年前

    someClass.newInstance();
    
        13
  •  4
  •   ryanprayogo    15 年前

    SomeClass anObj = SomeClass.class.newInstance();
    

        14
  •  4
  •   Bozho    14 年前
    • new
    • clazz.newInstance() clazz.getConstructor(..).newInstance(..)

        15
  •  3
  •   Roman    15 年前

    Foo fooClone = fooOriginal.clone (); 
    
        16
  •  3
  •   ServAce85    13 年前

    MyObject object = new MyObject();//normal way
    

    ClassName ObgRef=ClassName.FactoryMethod();
    

    RunTime rt=Runtime.getRunTime();//Static Factory Method
    

    clone()

    MyObjectName anotherObject = new MyObjectName();
    MyObjectName object = anotherObjectName.clone();//cloning Object
    

    MyObjectName object = (MyObjectNmae) Class.forName("PackageName.ClassName").newInstance();
    

    String st=(String)Class.forName("java.lang.String").newInstance();
    

    ObjectInputStreamName inStream = new ObjectInputStreamName(anInputStream );
    MyObjectName object = (MyObjectNmae) inStream.readObject();
    
        17
  •  1
  •   user207421    10 年前

    Employee object = new Employee();
    

    Employee object2 = (Employee) Class.forName(NewEmployee).newInstance();
    

    Employee secondObject = new Employee();
    Employee object3 = (Employee) secondObject.clone();
    

    Object object4 = Employee.class.getClassLoader().loadClass(NewEmployee).newInstance();
    

    // Create Object5
    // create a new file with an ObjectOutputStream
    FileOutputStream out = new FileOutputStream("");
    ObjectOutputStream oout = new ObjectOutputStream(out);
    
    // write something in the file
    oout.writeObject(object3);
    oout.flush();
    
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("crunchify.txt"));
    Employee object5 = (Employee) ois.readObject();
    
        18
  •  0
  •   Fabian Steeg    17 年前

        19
  •  -1
  •   Garth Gilmour    17 年前

        20
  •  -2
  •   Randy L    17 年前

        21
  •  -3
  •   Garth Gilmour    8 年前

        22
  •  -5
  •   Deepak Sharma    11 年前

    String s ="Hello";