instantiation

Pattern for lazy thread-safe singleton instantiation in java

Hello guys, the lazy thread-safe singleton instantion is kinda not easy to understand to every coder, so i wanted to create a class in our enterprise framework that would do the job. What do you think about it? Do you see something bad about it? Is there something similar like in Apache Commons? How can i make it better? Supplier.java...

python - strange behavior question

>>> class S(object): ... def __init__(self): ... self.x = 1 ... def x(self): ... return self.x ... >>> s = S() >>> s.x 1 >>> s.x() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not callable Why, in this example, is s.x a method, but also an integer? I...

How to safely and dynamically create an instance of an existing PHP class?

Say I have a file Foo.php: <?php interface ICommand { function doSomething(); } class Foo implements ICommand { public function doSomething() { return "I'm Foo output"; } } ?> If I want to create a class of type Foo I could use: require_once("path/to/Foo.php") ; $bar = new Foo(); But say that I've create...

HaXe -- create an instance of a class from a string name

Let's say i acquire the name of a class that i made as a String. How can i Instantiate the class with the name contained in that string? I I know it will be derived from a certain parent class, but the actual class will vary. ...

What's a good toString method for a deftype'd object in clojure

(deftype Bag [state] Object (toString [bag] (str "Bag???" state))) I want the toString to look something like clojure.core=> (def b (Bag. {:apples 1 :bannanas 4})) #'clojure.core/b clojure.core=> (str b) "BAG: {:apples 1 :bannanas 4}" What is a nice clojurey way of representing that information? Is "Bag/{:k :v}" ...

C++ template static member instantiation

#include <map> #include <iostream> template <typename T> class A { static std::map<int, int> data; public: A() { std::cout << data.size() << std::endl; data[3] = 4; } }; template <typename T> std::map<int, int> A<T>::data; //std::map<int, int> A<char>::data; A<char> a; int main() { return 0; } What is wrong with this? Wit...

How to create a new instance of the same class as the other object?

Having a object x which is an instance of some class how to create a new instance of the same class as the x object, without importing that all possible classes in the same namespace in which we want to create a new object of the same type and using isinstance to figure out the correct type. For example if x is a decimal number: >>> fr...

How do I instantiate a class that only has a private parameterless constructor in Silverlight?

I'm working on a feature request for a .NET test data builder. I need to be able to instantiate classes that only have private parameterless constructors. For instance, I might need to instantiate the following class: public class MyClass() { private MyClass(){} } For most other classes I use the following code: (T)Activator.Cr...

How can I instantiate a class using the shorthand C# way?

This is the class I'm trying to instantiate: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { public class Posicion { public int X { get; set; } public int Y { get; set; } } } And here I'm trying to create it: button1.Tag = new Posicion() { 1, 1 }; I...

How to Initialize a Instance of a PHP Class using another Object Instance?

What would be a good way (along with any pros and cons) of initializing an instance of a PHP class with another object of the same class (ideally in PHP 4.x)? Here in initialize() is essentially what I'd like to be able to do (example is extremely simplified from my use-case, see below): $product = new Product('Widget'); $product2 = ne...

Instantiate interface in JAVA?

I'm reading the docs on the UIBinder of GWT and the first code snippet made me confused: public class HelloWorld extends UIObject { // Could extend Widget instead interface MyUiBinder extends UiBinder<DivElement, HelloWorld> {} private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class); @UiField SpanElement nameSpan; pu...

Instantiating two objects inside a method for use as 'return values'

I've got the following class: public class Matriz { ... static public void create(Matriz a, Matriz b) { ... a=new Matriz(someValue,anotherValue); b=new Matriz(someValue,anotherValue); ... } } And in my main method: public static void main(String[] args) { Matriz a=null,b=null; Matriz.create(a,b); ...

Using *this in C++ class method to fully overwrite self instantiation

Is the following code safe? (I already know it compiles properly.) void Tile::clear() { *this = Tile(); } int main() { Tile mytile; mytile.clear(); } ...

JPA+Address Book Demo+Netbeans 6.9.1 problem

I'm trying to include persistence to a Vaadin project so my first step was to try the JPA+Address Book Demo using Netbeans. When I try to run the demo I get a "StandardWrapperValve[VaadinApplication]: PWC1406: Servlet.service() for servlet VaadinApplication threw exception". I tried by adding gwt-user.jar but it didn't work. My knowledg...

Ruby syntax question: Rational(a, b) and Rational.new!(a, b)

Today I came across the strange ruby syntax in the Rational class: Rational(a,b) (Notice the absence of the .new()portion compared to the normal Ruby syntax). What does this mean, precisely, compared to the normal new syntax? More importantly, how do I implement something like this in my own code, and why would I implement something...

Error CS0563 (One of the parameters of a binary operator must be the containing type)

My source code (below) is generating Error CS0563 because both of the parameters in my CombinedJobs operator+ (see "//Step 5: ..." in source code) are listed as Job (as opposed to int, double, etc). How can I change this code to eliminate this error? This code is in response to the assignment listed below. Assignment: "Design a Job cla...