FAQ Database Discussion Community


Can a constructor NOT return a new instance?

java,instantiation
Is it possible to have a constructor decide, based on the argument, not to make a new instance? For example: public class Foo{ public Foo(int n){ if(n<0){ //DO NOT make this } else{ //Go ahead and make this instance } } } I know it is not possible to do...

Java Constructor Injection

java,constructor,dependency-injection,instantiation,constructor-injection
Let's say I have a class resources which instantiates all of my OpenGL / Java game objects and I then pass these via constructor to my Scene class (which requires them), like so (simplified example)..... public class Resources { Hero hero; Enemy enemy; MenuButtons mainMenuButtons; Background background; Scene mainMenu; public...

instantiating an inner class from the main method

instantiation,inner-classes
How can I instantiate an inner class? for example: QuoteArray quotes1 = new QuoteArray(1); QuoteArray.Quote quote1 = new QuoteArray.Quote("quote","char","actor","film","year");//error here Where Quote is an inner class of QuoteArray. None of this really makes sense to me. I know how to instantiate Quote from within QuoteArray, but I don't know how...

Create new instance of MailAddress using stored variables instead of strings for parameters

c#,.net,asp.net-mvc,parameters,instantiation
Could you help me understand why this isn't working? Because I'm unable to create a new MailAddress object using variables in place of the strings that are normally assigned in the Display and Address parameters. If the following code is used then the email goes through fine from "The Example...

How to instantiate an object from a stored Class?

objective-c,class,instantiation
I have an function which gives me back a Class: - (Class)classForMyType:(NSString *)myType; This class is given back like this: return [MyFirstClass class]; In the code, where I call this function, I can store the Class like this: Class myClass = [self classForMyType:@"foo"]; How can I instantiate a variable in...

viewController lifecycle

ios,uiviewcontroller,instantiation,lifecycle
Apple's documentation says "When a segue is triggered, it causes a new view controller to be instantiated and transitioned onscreen." Some behavior I'm seeing has me questioning if the VC is actually destroyed and recreated each time the view is seque'd to. In my application each time I seque to...

Laravel 4.2: Troubleshooting “Target not instantiable” error

php,laravel,ioc-container,instantiation
I've been stuck on this "Target not instantiable" error for the last 2-3 days and I have no idea why. Someone in the IRC #laravel chat room, yesterday, suggested using var_dump(App::make('Project\Frontend\Repo\Lead\LeadInterface')) when in Artisan's tinker interface which I've done and got this response: class Project\Frontend\Repo\Lead\EloquentLead#798 (1) { protected $lead =>...

Difference between two types of a super class instantiation in Java [duplicate]

java,object,java-ee,instantiation,superclass
This question already has an answer here: Java: difference between A x = new A() and A x = new B() when B extends A [duplicate] 6 answers I have an abstract super class called Document and two child classes called Magazine and Book. What is the difference between...

Python: Is there a way to update the keys (columns) of a DictWriter before calling writerow()?

python,list,csv,dictionary,instantiation
csv.Dictwriter is instantiated thus: class csv.DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds) fieldsnames is a sequence of keys, or in my case, a dict. Here's my question; after instantiating DictWriter with a certain dict, I wish to remove some of the keys (and by extension, values) of fieldnames before calling...

F# declare variable without instatiating it

f#,scope,initialization,instantiation,declare
I need to declare a variable in F# without instantiating it. This is simple in C#, but in F#, all I have is the val keyword, and that only works within classes. Is there a way to declare a variable for scope purposes, but to leave the initialization for later?...

How to allow third party to extend classes in my java library

java,inheritance,instantiation
I have a parent class in java with some behavior defined public class ParentClass { public ParentClass() { //Do some instantiation } public void doWork() { //Do some behavior defined by the parent class } } Now I have some other class in my project that uses this. Let's say...

Does { } act like ( ) when creating a new object in C#?

c#,.net,instantiation
I just noticed that using {} instead of () gives the same results when constructing an object. class Customer { public string name; public string ID {get; set;} } static void Main() { Customer c1= new Customer{}; //Is this a constructor? Customer c2= new Customer(); //what is the concept behind...

In a loop in Python, I assign a new instance of a class to the same variable, but it keeps pointing to the old instance? [duplicate]

python,class,object,instantiation
This question already has an answer here: How do I avoid having Python class data shared among instances? 6 answers I created the following class that represents a chest where toys (numbers) can be stored: class Chest: toys = [] def __init__(self): return def add(self, num): self.toys.append(num) return The...

In Java, how can I manipulate a variable instantiated from another class?

java,instantiation
I am very new to Java and am making a simple application. That consists of two classes, Computer and Player. In Player, I prompt the user to guess a five digit string. In Computer, I instantiate the Player class as so: Player number = new Player(); number.getGuess(); Next, I created...

Why is [] faster than list()?

python,performance,list,instantiation,literals
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 around 0.128sec / million cycles,...

instantiating a composite class from an instance method Ruby

ruby,instantiation,composite
I'm playing with ways of enforcing class relationships in Ruby before I start writing out the code over the weekend. I'm trying for a composite relationship, but one that I have some measure of control over the PriceSeries class from outside the Commodity class. Obviously this isn't it, but is...

How to fill HashMap object only once for program?

java,static,hashmap,instantiation
Given the following portion of my wider program: public class AnimalNames { private static String[] animalNames = {dog, cat, horse, cow, donkey, elephant}; private static String[] animalNameAbbreviations = {d, c, h, co, d, e}; public static HashMap<String, String> getAnimalNameTranslations() { HashMap<String, String> animalNameTranslations = new HashMap<String, String>(); for (int i...

Template class's static variable initialization, c++

c++,templates,static,instantiation,linkage
consider the following code: //header.h template<class T> class A { static int x; }; template<class T> int A<T>::x = 0; //source1.cpp #include "header.h" void f(){} // dummy function //main.cpp #include "header.h" int main(){} In this case code compiles perfectly without errors, but if I remove the template qualifier from class...

How does this Java code instantiate an abstract class?

java,class,syntax,instantiation,abstract
I am making changes to a Java class of ours, and I noticed the following line of code: OurClass<OurInterface1> ourClass = new OurClass<OurInterface1>() {}; What I find strange about that line is that OurClass is an abstract class - here's the definition of OurClass: public abstract class OurClass<T extends OurInterface1>...

initializing python object with nothing but keyword args

python,object,constructor,instantiation
I am wondering how to initialize an object in python with only named args, and nothing else, if this is possible. If the order of self.name = name and self.age = age are swapped, the error is with initializing age. I have these given as keyword args to the object,...

Meteor - How to call an external class methods from other JS files?

javascript,oop,meteor,instantiation
I'm new to meteor. I've been trying to write my meteor code the "object-oriented way" so I created an object called Message for my Parser and called it's methods from my app.js. I would like to instantiate the object and call the methods defined in my Message.JS from my app.js?...

Inner Class Instantiate Clarification

java,swing,instantiation,inner-classes
I don't understand a feature of the inner class CHameleon. I don't get what line 8 in theory means. I think it means to produce a duplicate version of JFrame that will be accessed outside of the inner class, in order to manipulate the intended JFrame object. Edit: Code brings...

How to instantiate a new object from an abstract class containing an instance of a HashMap? [closed]

java,polymorphism,instantiation,abstract
I believe that abstract classes cannot be instantiated. However, I have a HashMap instance inside my abstract class that I want to retrieve objects from. Is there any way possible to do this? Any insight would be appreciated, thanks in advance! Edit: I am actually looking for a way to...

ruby - calling a method on a dynamically named object

ruby,object,dynamic,instantiation
I have an array of strings, that represent existing object names. JoesDev = Dev.new MarksDev = Dev.new SamsDev = Dev.new devices=['JoesDev', 'MarksDev', 'SamsDev' ] i'd like to iterate over the devices array, while calling a method on the objects that each item in the array is named after. i.e; JoesDev.method_name...

Which is really a object?

java,class,object,instantiation
So I have a problem trying to figure out if some Object is really created or it's just some kind of pointer. Can someone help me understand that please? I have this kind of example if it helps :) Bookshelf a = new Bookshelf(2); Bookshelf b = a; Bookshelf c...

Exception when registering service and Broadcastreceiver

android,service,broadcastreceiver,instantiation,runtimeexception
I have two classes 1)public class MainActivity extends Service { 2)public class Boot extends BroadcastReceiver { How can i register these in Manifestfile. <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> when MainActivity is registered like this it shows an exception. java.lang.RuntimeException: Unable to instantiate...

Can I use decltype (or something similar) for explicit template instantiation without signature duplication?

c++,templates,c++11,instantiation,decltype
I want to instantiate template<typename T> void foo( T& t, SomeType some_parameter, AnotherType another_parameter, EtcType yet_another_parameter, AsYouCanTell this_is_a_very_long_signature); that is, a function with a long signature. Now, I know how to do this: template void foo<int>( int& t, SomeType some_parameter, AnotherType another_parameter, EtcType yet_another_parameter, AsYouCanTell this_is_a_very_long_signature); But I have to...

Filling a List> with values

java,arraylist,instantiation
So I've been running into a few errors. When I try to add an event to a date in a month, I try to add an event into the date ArrayList but the list doesn't have that number available. So I want to know how to instantiate a list that...

Right way to instantiate GameObject?

c#,unity3d,instantiation,gameobject
So I have a dilemma about right way to instantiate prefab gameObject in Unity c#. Here is the 2 example codes Example 1: public Transform musicPrefab; GameObject mManager = (GameObject)Instantiate(musicPrefab,transform.position, Quaternion.identity); Example 2: public Transform musicPrefab; Object mManager = Instantiate(musicPrefab,transform.position, Quaternion.identity); But instantiating as Object works as well, so which...