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
43
votes
4 answers

How to instantiate an object with a private constructor in C#?

I definitely remember seeing somewhere an example of doing so using reflection or something. It was something that had to do with SqlParameterCollection which is not creatable by a user (if I'm not mistaken). Unfortunately cannot find it any…
User
  • 30,403
  • 22
  • 79
  • 107
43
votes
4 answers

How to Properly Declare Array of Custom Objects in Swift?

Here is my custom class...not sure if I'm missing anything in it... import UIKit class baseMakeUp { var Image = UIImage() var Brand: String var Color: String var Rating: Int = 0 init (Brand: String, Color: String) { …
jbd36
  • 493
  • 1
  • 4
  • 8
42
votes
3 answers

Call a model method in a Controller

I'm have some difficulties here, I am unable to successfully call a method which belongs to a ProjectPage model in the ProjectPage controller. I have in my ProjectPage controller: def index @searches = Project.published.financed …
sidney
  • 2,704
  • 3
  • 28
  • 44
39
votes
5 answers

What exactly is "broken" with Microsoft Visual C++'s two-phase template instantiation?

Reading questions, comments and answers on SO, I hear all the time that MSVC doesn't implement two-phase template lookup / instantiation correctly. From what I understand so far, MSVC++ is only doing a basic syntax check on template classes and…
Xeo
  • 129,499
  • 52
  • 291
  • 397
39
votes
7 answers

Create Annotation instance with defaults, in Java

How can I create an instance of the following annotation (with all fields set to their default value). @Retention( RetentionPolicy.RUNTIME ) public @interface Settings { String a() default "AAA"; String b() default…
akuhn
  • 27,477
  • 2
  • 76
  • 91
39
votes
5 answers

Can a static nested class be instantiated in Java?

From Oracle's Java tutorials I've found this text: As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or…
user1031431
  • 1,475
  • 6
  • 17
  • 24
36
votes
5 answers

Does System.Activator.CreateInstance(T) have performance issues big enough to discourage us from using it casually?

Does System.Activator.CreateInstance(T) method have performance issues (since I'm suspecting it uses reflection) big enough to discourage us from using it casually?
Pacerier
  • 86,231
  • 106
  • 366
  • 634
34
votes
13 answers

Can I use methods of a class without instantiating this class?

I have a class with several methods and there is no constructor among these methods. So, I am wondering if it is possible to call a method of a class without a creation of an instance of the class. For example, I can do something like…
Roman
  • 124,451
  • 167
  • 349
  • 456
31
votes
6 answers

PHP - best way to initialize an object with a large number of parameters and default values

I'm designing a class that defines a highly complex object with a ton (50+) of mostly optional parameters, many of which would have defaults (eg: $type = 'foo'; $width = '300'; $interactive = false;). I'm trying to determine the best way to set up…
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
30
votes
7 answers

Why is it possible to instantiate a struct without the new keyword?

Why are we not forced to instantiate a struct, like when using a class?
Birdman
  • 5,244
  • 11
  • 44
  • 65
30
votes
4 answers

Get Enum Instance from Class using String value?

I'm finding it difficult to put the exact question into words, so I'll just give an example. I have two Enum types: enum Shape { CAT, DOG; } enum Color { BLUE, RED; } I have a method: public Object getInstance(String value, Class
Craig Otis
  • 31,257
  • 32
  • 136
  • 234
28
votes
2 answers

how to instantiate an object of class from string in Objective-C?

I've a String who's value is the name of the Class[MyClass] which has to be instantiated, and MyClass has a method called -(void)FunctionInClass; i'm using the method called NSClassFromString to instantiate MyClass.I want to know 1) what does…
suse
  • 10,503
  • 23
  • 79
  • 113
27
votes
2 answers

Why can private member variable be changed by class instance?

class TestClass { private string _privateString = "hello"; void ChangeData() { TestClass otherTestClass = new TestClass(); otherTestClass._privateString = "world"; } } This code compiles in C# and the equivalent…
Michael Low
  • 24,276
  • 16
  • 82
  • 119
26
votes
2 answers

instantiate chrome object in powershell

I have a powershell script which works just fine in IE however I need to also have it work in Chrome. $ie = new-object -com "InternetExplorer.Application" Works fine for IE. How do I instantiate the Chrome browser?
user2874251
  • 361
  • 1
  • 3
  • 3
26
votes
6 answers

Generics and Class.forName

I would like to create an instance of a specified class using its name. My code is shown below. I get a compiler warning. Am I doing this the right way? Is it even possible to use the name of a class and get an instance of that type back, as I don't…
dogbane
  • 266,786
  • 75
  • 396
  • 414