Questions tagged [instance]

In object-oriented programming an instance is an occurrence or a copy of an object, whether currently executing or not.

Instances of a class share the same set of attributes, yet will typically differ in what those attributes contain. For example, a class "Employee" would describe the attributes common to all instances of the Employee class. For the purposes of the task being solved Employee objects may be generally alike, but vary in such attributes as "name" and "salary".

Related tags:

5388 questions
36
votes
3 answers

How to test same object instance in Javascript?

Say I have the following objects in Javascript: var a = { xxx: 33 }; var b = { xxx: 33 }; var c; c = a; What is the Javascript test that will tell me whether I am dealing with the same object instance? In other words, it should return false for a…
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
35
votes
6 answers

Create an instance of a React class from a string

I have a string which contains a name of the Class (this is coming from a json file). This string tells my Template Class which layout / template to use for the data (also in json). The issue is my layout is not displaying. Home.jsx: //a template or…
ewan
  • 454
  • 1
  • 4
  • 9
35
votes
9 answers

Easiest way to copy/clone a mongoose document instance?

My approach would be to get the document instance, and create a new one from the instance fields. I am sure there is a better way to do it.
fusio
  • 3,595
  • 6
  • 33
  • 47
34
votes
2 answers

How to copy/create derived class instance from a pointer to a polymorphic base class?

I have been struggling with this kind of problem for a long time, so I decided to ask here. class Base { virtual ~Base(); }; class Derived1 : public Base { ... }; class Derived2 : public Base { ... }; ... // Copies the instance of derived class…
eold
  • 5,972
  • 11
  • 56
  • 75
34
votes
4 answers

Setting default/empty attributes for user classes in __init__

I have a decent level of programming, and get much value from the community here. However I have never had much academic teaching in programming nor worked next to really experienced programmers. Consequently I sometimes struggle with 'best…
Andy
  • 919
  • 2
  • 9
  • 22
33
votes
4 answers

python class instance variables and class variables

I'm having a problem understanding how class / instance variables work in Python. I don't understand why when I try this code the list variable seems to be a class variable class testClass(): list = [] def __init__(self): …
jonathan topf
  • 7,897
  • 17
  • 55
  • 85
33
votes
6 answers

Can we create an object of an interface?

interface TestA { String toString(); } public class Test { public static void main(String[] args) { System.out.println(new TestA() { public String toString() { return "test"; } }); …
shirsendu shome
  • 385
  • 1
  • 5
  • 4
33
votes
3 answers

How many instances created for singleton bean referring to a session bean/prototype bean

I have a doubt about the number of instances that will be created in the scenario mentioned below, when Spring Framework is used: The bean configuration is like this
user1477232
  • 457
  • 1
  • 8
  • 17
33
votes
5 answers

Static classes in Python

I once read (I think on a page from Microsoft) that it's a good way to use static classes, when you don't NEED two or more instances of a class. I'm writing a program in Python. Is it a bad style, if I use @classmethod for every method of a class?
rynd
  • 1,865
  • 5
  • 22
  • 24
32
votes
12 answers

How can I tell if another instance of my program is already running?

How do i tell if one instance of my program is running? I thought I could do this with a data file but it would just be messy :( I want to do this as I only want 1 instance to ever be open at one point.
Arthur
  • 3,376
  • 11
  • 43
  • 70
32
votes
2 answers

Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?

I have this code (the whole code is not important but can be seen on this link): internal static class PlayCardActionValidator { public static bool CanPlayCard(...) { // ... var hasBigger = playerCards.Any( …
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
32
votes
8 answers

Performance of using static methods vs instantiating the class containing the methods

I'm working on a project in C#. The previous programmer didn't know object oriented programming, so most of the code is in huge files (we're talking around 4-5000 lines) spread over tens and sometimes hundreds of methods, but only one class.…
deadtime
  • 1,179
  • 1
  • 12
  • 19
32
votes
2 answers

Python : Assert that variable is instance method?

How can one check if a variable is an instance method or not? I'm using python 2.5. Something like this: class Test: def method(self): pass assert is_instance_method(Test().method)
quano
  • 18,812
  • 25
  • 97
  • 108
31
votes
8 answers

How to keep track of class instances?

Toward the end of a program I'm looking to load a specific variable from all the instances of a class into a dictionary. For example: class Foo(): def __init__(self): self.x = {} foo1 = Foo() foo2 = Foo() ... Let's say the number of…
DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
30
votes
4 answers

How to create a new instance of a struct

What is the correct way to create a new instance of a struct? Given the struct: struct listitem { int val; char * def; struct listitem * next; }; I've seen two ways.. The first way (xCode says this is redefining the struct and…
Robin Huang
  • 383
  • 2
  • 4
  • 6