instantiation

Is there an elegant way to instantiate a variable type with parameters?

This isn't legal: public class MyBaseClass { public MyBaseClass() {} public MyBaseClass(object arg) {} } public void ThisIsANoNo<T>() where T : MyBaseClass { T foo = new T("whoops!"); } In order to do this, you have to do some reflection on the type object for T or you have to use Activator.CreateInstance. Both are pretty nas...

JUnit TestCase object instantiation

Is a new (or different) instance of TestCase object is used to run each test method in a JUnit test case? Or one instance is reused for all the tests? public class MyTest extends TestCase { public void testSomething() { ... } public void testSomethingElse() { ... } } While running this test, how many instances of MyTest class is ...

Possible circular dependency issue with PHP application

I'm experiencing what I believe is a circular dependency issue with my PHP application. Please let me know if this is incorrect. Here is the situation: Two classes, LogManager and DBSession. DBSession is used to interact with the database, and LogManager is used to log to files. Both are widely used in my application. When you create a...

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<? extends Runnable> ctor = runClass.getConstructor(); Runnable doRun = ctor.newInstance(); so, why is it Evil? ...

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 "BBB"; String c() default "CCC"; } I tried new Settings(), but that does ...

KeyListener in Java is abstract; cannot be instantiated?

Hey all, I am trying to create a Key Listener in java however when I try KeyListener listener = new KeyListener(); Netbeans is telling me that KeyListener is abstract;cannot be instantiated. I know that I am missing some other piece of this key listener, but since this is my first time using a key listener i am unsure of what else i...

calling a .jar from Coldfusion

I have a .jar file that i've placed in my D:\Coldfusion8\wwwroot\web-inf\lib\ directory. The file is abcdef.jar This is my first time doing this, not sure what the path should be in my createObject(). <cfset abcObj = createObject("java","com.abcdef") /> <cfset result = acbObj.doStuff("123456") /> But when I run it, I get Object In...

C# - Instantiate a class depending on a provided interface in a generic class

Hi, I have a generic function which gets a interface as a type, now in one condition I have to create a new class depending on the interface. I have been thinking about it and a way to solve it would be to use an IoC but I was hoping there would be an other way because an IoC seems a bit like an overkill. below is an attempt using the...

C++ Object Instantiation

Hi all, 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 memory allocation is unnecessary, is there any reason to use the above inste...

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 = typeof(Sometype); object o = Activator.CreateInstance(t); Normally this will work, however becaus...

Quick Java Question: Instantiating a given class only from another?

My problem is thus: I need a way to ensure only one given class can instantiate another. I don't want to have to make the other a nested inner class or something dumb like that. How do I do this? I forget offhand. ...

Addressing instance name string in __init__(self) in Python.

I am doing something like this: class Class(object): def __init__(self): self.var=#new instance name string# How do I make the __ init __ method of my instance to use the instance name string for 'c'? Say in case: c=Class() I want c.var equal to 'c'. Thanks for your replies, I am implementing persistence and Class is p...

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

I have the need to take a string argument and create a class 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 to know what I'm doing: I want to use a command line argument as the class name, and instantiate it. I'm actually programmi...

Possible Multi-Instanced Object Problem in PHP

Hi, I'm getting a "Call to a member function process_data() on a non-object in page.class.php on line 35" even though the object has been called. Here is the index.php extraction showing the object being instantised // require our common files require("modules/module.php"); require("registry/objects/datetime.class.php"); require("reg...

Visual Studio WinForms designer does not instantiate object

I created a class derived from System.Windows.Forms.ContextMenuStrip class, not as a user control, just a plain .cs class with a constructor and one event handler. When I drag this class from the Toolbox onto the designer, it creates a private member for it and a couple of properties, but does not instantiate an object. Thus, at runtim...

What does Class.fromName() not work for me?

Hello, I'm trying to instantiate a class from variable, and written a test script. But, unfortunately, it isn't woriking. There is that script: Object co1 = new CommandDownloadHttp(); Class cc1 = Class.forName("CommandDownloadHttp"); Object co = cc1.newInstance(); Unfortunately on second line it crashes with java.lang.ClassNotFoundEx...

Why should/shouldn't I use the "new" operator to instantiate a class, and why?

I understand that this may be construed as one of those "what's your preference" questions, but I really want to know why you would choose one of the following methods over the other. Suppose you had a super complex class, such as: class CDoSomthing { public: CDoSomthing::CDoSomthing(char *sUserName, char *sPassword) ...

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

Hi guys, 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 }; Base * Create() { if(msClassName == "DerivedA") { return new DerivedA(); } else i...

Automatically separate class definitions from declarations?

I am using a library that consists almost entirely of templated classes and functions in header files, like this: // foo.h template<class T> class Foo { Foo(){} void computeXYZ() { /* heavy code */ } }; template<class T> void processFoo(const Foo<T>& foo) { /* more heavy code */ } Now this is bad because compile times are unbearab...

What's the difference between dict() and {}?

So let's say I wanna make a dictionary. We'll call it d. But there are multiple ways to initialize a dictionary in Python! For example, I could do this: d = {'hash': 'bang', 'slash': 'dot'} Or I could do this: d = dict(hash='bang', slash='dot') Or this, curiously: d = dict({'hash': 'bang', 'slash': 'dot'}) Or this: d = dict([['...