Ways to Create Instance

What are all the different ways to create an object in Java?

There are four different ways to create objects in java:

A. Using new keyword

This is the most common way to create an object in java. Almost 99% of objects are created in this way.

 MyObject object = new MyObject();

B. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.

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

C. Using clone()
The clone() can be used to create a copy of an existing object.

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

D. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.

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

You can read them from here.

Different way to create an instance method object in Python

I'm not 100% sure that I completely understand your question, but maybe looking at an example will be helpful. To start, lets create a class which has a function in the definition:

>>> class Foo(object):
... def method(self):
... pass
...
>>> f = Foo()

User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object or a class method object.

Ok, so we can create a method object by just accessing the attribute on an instance (if the attribute is a function). In our setup, f is an instance of the class Foo:

>>> type(f.method)
<class 'method'>

Compare that with accessing the method attribute on the class:

>>> type(Foo.method)
<class 'function'>

When an instance method object is created by retrieving a user-defined function object from a class via one of its instances, its __self__ attribute is the instance, and the method object is said to be bound. The new method’s __func__ attribute is the original function object.

This is just telling us what attributes exist on instance methods. Let's check it out:

>>> instance_method = f.method
>>> instance_method.__func__ is Foo.method
True
>>> instance_method.__self__ is f
True

So we see that the method object has a __func__ attribute which is just a reference to the actual Foo.method function. It also has a __self__ attribute that is a reference to the instance.

When an instance method object is called, the underlying function (func) is called, inserting the class instance (self) in front of the argument list. For instance, when C is a class which contains a definition for a function f(), and x is an instance of C, calling x.f(1) is equivalent to calling C.f(x, 1).

Basically, in reference to our example above, this is just saying that If:

instance_method = f.method

Then:

instance_method(arg1, arg2)

executes the following:

instance_method.__func__(instance_method.__self__, arg1, arg2)

how to create in best way an instance from the class object

Leave your Dog and Animal classes the same. and use the Builder pattern

  public interface Builder<T> {
public T build(String nameString);
}
public static void main(String[] args){
Builder<Dog> builder = new Builder<Dog>()
{

@Override
public Dog build(String nameString)
{
return new Dog(nameString);
}

};
Dog dog = builder.build("Rocky");
System.out.print(dog.name);

}

The answers over at Instantiating object of type parameter explain it further as well.

How to create instance of model instance in JavaScript?

A little confused by this question. Could you elaborate a little more if this doesn't answer your question.

I would also note that you should be extremely (and I mean EXTREMELY) careful with giving users this much power. Especially if you will be letting them directly interact with your DB in the example you provider. Im still not entire sure why you would want to do this and would consider other options first.

Anyway that being said, as to the whole creating instances of an instance, that is totally possible. Objects are objects are objects. The class you created is an object, which creates objects. In Javascript we only really have prototypal inheritance. So lets say you have and instance of a class, and you want to create a unqiue instance of that instance you can use Object.create(instance). This with return you an object, with its prototype pointing to the instance you originally passed.

For the first part of your question, still a little confused on the wording but I'm assuming that you want to create a way for users to create a new instance of the model class. Im also assuming that there will be alot more functionality on this model class. I would recommend creating a API class to interact and 'manage' these models. So you would create a new class that has a method on it which when run invokes new Model(args). Let me know if that was what you were looking for.

Alright - So if you wanna use both you could use a function constructor:

function SomeClass() {
someObj = {};
someObj.blah = 'a';
someObj.func = function() {
console.log(this.blah);
}
return someObj;
}

You could use this with new SomeClass() and SomeClass()



Related Topics



Leave a reply



Submit