Questions tagged [instantiation]

Instantiation is the process of creating objects from a class in most object oriented and object based languages. In the C++ language, instantiation is the process of creating a class or function from a class template or function template.

2609 questions
1230
votes
32 answers

How to create a generic array in Java?

Due to the implementation of Java generics, you can't have code like this: public class GenSet { private E a[]; public GenSet() { a = new E[INITIAL_ARRAY_LENGTH]; // error: generic array creation } } How can I implement…
tatsuhirosatou
  • 25,149
  • 14
  • 39
  • 40
795
votes
5 answers

Why is [] faster than list()?

I recently compared the processing speeds of [] and list() and was surprised to discover that [] runs more than three times faster than list(). I ran the same test with {} and dict() and the results were practically identical: [] and {} both took…
Augusta
  • 7,171
  • 5
  • 24
  • 39
375
votes
10 answers

Creating an instance using the class name and calling constructor

Is there a way to create an instance of a particular class given the class name (dynamic) and pass parameters to its constructor. Something like: Object object = createInstance("mypackage.MyClass","MyAttributeValue"); Where "MyAttributeValue" is an…
TheLameProgrammer
  • 4,037
  • 5
  • 19
  • 16
270
votes
8 answers

Create an instance of a class from a string

Is there a way to create an instance of a class based on the fact I know the name of the class at runtime. Basically I would have the name of the class in a string.
PeteT
  • 18,754
  • 26
  • 95
  • 132
192
votes
8 answers

Instance member cannot be used on type

I have the following class: class ReportView: NSView { var categoriesPerPage = [[Int]]() var numPages: Int = { return categoriesPerPage.count } } Compilation fails with the message: Instance member 'categoriesPerPage' cannot be used on…
Aderstedt
  • 6,301
  • 5
  • 28
  • 44
178
votes
9 answers

What is the difference between "Class.forName()" and "Class.forName().newInstance()"?

What is the difference between Class.forName() and Class.forName().newInstance()? I do not understand the significant difference (I have read something about them!). Could you please help me?
Johanna
  • 27,036
  • 42
  • 89
  • 117
161
votes
5 answers

How do I write a custom init for a UIView subclass in Swift?

Say I want to init a UIView subclass with a String and an Int. How would I do this in Swift if I'm just subclassing UIView? If I just make a custom init() function but the parameters are a String and an Int, it tells me that "super.init() isn't…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
160
votes
12 answers

Is there a way to instantiate objects from a string holding their class name?

I have a file: Base.h class Base; class DerivedA : public Base; class DerivedB : public Base; /*etc...*/ and another file: BaseFactory.h #include "Base.h" class BaseFactory { public: BaseFactory(const string &sClassName){msClassName =…
Gal Goldman
  • 8,641
  • 11
  • 45
  • 45
126
votes
9 answers

Is there a way to instantiate a class by name in Java?

I was looking as the question : Instantiate a class from its string name which describes how to instantiate a class when having its name. Is there a way to do it in Java? I will have the package name and class name and I need to be able to create an…
ict1991
  • 2,060
  • 5
  • 26
  • 34
114
votes
9 answers

C++ Object Instantiation

I'm a C programmer trying to understand C++. Many tutorials demonstrate object instantiation using a snippet such as: Dog* sparky = new Dog(); which implies that later on you'll do: delete sparky; which makes sense. Now, in the case when dynamic…
el_champo
  • 1,219
  • 3
  • 10
  • 8
110
votes
7 answers

Does python have an equivalent to Java Class.forName()?

I have the need to take a string argument and create an object of the class named in that string in Python. In Java, I would use Class.forName().newInstance(). Is there an equivalent in Python? Thanks for the responses. To answer those who want…
Jason
  • 1,418
  • 2
  • 13
  • 10
107
votes
5 answers

base() and this() constructors best practices

Under what conditions am I supposed to make the :base() and :this() constructor calls following my constructor's parentheses (or even in other places in the code). When are these calls good practices and when are they mandatory?
explorer
  • 11,710
  • 5
  • 32
  • 39
106
votes
3 answers

`new function()` with lower case "f" in JavaScript

My colleague has been using "new function()" with a lower case "f" to define new objects in JavaScript. It seems to work well in all major browsers and it also seems to be fairly effective at hiding private variables. Here's an example: var…
Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
105
votes
4 answers

Creating instance of type without default constructor in C# using reflection

Take the following class as an example: class Sometype { int someValue; public Sometype(int someValue) { this.someValue = someValue; } } I then want to create an instance of this type using reflection: Type t =…
Aistina
  • 12,435
  • 13
  • 69
  • 89
100
votes
3 answers

Why is Class.newInstance() "evil"?

Ryan Delucchi asked here in comment #3 to Tom Hawtin's answer: why is Class.newInstance() "evil"? this in response to the code sample: // Avoid Class.newInstance, for it is evil. Constructor ctor =…
Amir Arad
  • 6,724
  • 9
  • 42
  • 49
1
2 3
99 100