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
60
votes
2 answers

new Backbone.Model() vs Backbone.Model.extend()

I'm new to JS and Backbone What's the difference between these two? TestModel = new Backbone.Model({ title: "test title" }) TestModel = Backbone.Model.extend({ title: "test title" })
crapthings
  • 2,485
  • 3
  • 20
  • 33
58
votes
12 answers

What exactly is an instance in Java?

What is the difference between an object, instance, and reference? They say that they have to create an instance to their application? What does that mean?
priya
  • 4,287
  • 7
  • 22
  • 15
54
votes
4 answers

Why do two programs have forward referencing errors while the third does not?

The following does not compile, giving an 'illegal forward reference' message: class StaticInitialisation { static { System.out.println("Test string is: " + testString); } private static String testString; public…
danger mouse
  • 1,457
  • 1
  • 18
  • 31
53
votes
4 answers

PHP check for instance of DateTime?

Is this the only way to check if an object is an instance of a class, in my case of the DateTime class? $cls = ReflectionClass("DateTime"); if (! $cls->isInstance( (object) $var ) ) { // is not an instance } It seems a bit heavy to me.
Niklas R
  • 16,299
  • 28
  • 108
  • 203
52
votes
6 answers

Method arguments in Python

Suppose I have this code: class Num: def __init__(self,num): self.n = num def getn(self): return self.n def getone(): return 1 myObj = Num(3) print(myObj.getn()) # result: 3 But if I try print(myObj.getone()), I…
Yugo Kamo
  • 2,349
  • 4
  • 18
  • 13
51
votes
8 answers

How to create a list of objects?

How do I go about creating a list of objects (class instances) in Python? Or is this a result of bad design? I need this cause I have different objects and I need to handle them at a later stage, so I would just keep on adding them to a list and…
user225312
  • 126,773
  • 69
  • 172
  • 181
47
votes
4 answers

Create class instance from within static method

As the title says, I'm wanting to create an instance of a class from within a static method of the same class. I've figured out so far is that I can by doing something like this: class Foo{ public $val; public static function bar($val){ $inst…
daniel
  • 1,148
  • 1
  • 13
  • 20
44
votes
6 answers

Java: newInstance of class that has no default constructor

I'm trying to build an automatic testing framework (based on jUnit, but that's no important) for my students' homework. They will have to create constructors for some classes and also add some methods to them. Later, with the testing functions I…
GermanK
  • 1,676
  • 2
  • 14
  • 21
43
votes
6 answers

How can I call a static method on a variable class?

I'm trying to make some kind of function that loads and instantiates a class from a given variable. Something like this:
Jelle Spekken
43
votes
3 answers

Using onCreate vs. onRestoreInstanceState

Is there technically any reason why I should use onRestoreInstanceState? Could I not do all the restoration in onCreate by checking if the savedInstanceState bundle is null? What is the primary benefit of using onRestoreInstanceState over doing…
Sean Hill
  • 1,297
  • 1
  • 13
  • 21
41
votes
11 answers

Determine if Python variable is an instance of a built-in type

I need to determine if a given Python variable is an instance of native type: str, int, float, bool, list, dict and so on. Is there elegant way to doing it? Or is this the only way: if myvar in (str, int, float, bool): # do something
Aleksandr Motsjonov
  • 1,230
  • 3
  • 14
  • 25
39
votes
6 answers

How to remove(unregister) registered instance from Unity mapping?

I meet one problem that i can't solve now. I have the following: UnityHelper.DefaultContainer.RegisterInstance(typeof(IMyInterface), "test", instance); where UnityHelper.DefaultContainer is my helper for getting unity container with loaded…
bug0r
  • 613
  • 1
  • 8
  • 19
38
votes
8 answers

C# Get property value without creating instance?

Is it possible to get value without creating an instance ? I have this class: public class MyClass { public string Name{ get{ return "David"; } } public MyClass() { } } Now I need get the value "David", without creating instance of…
user1475694
  • 415
  • 1
  • 4
  • 3
37
votes
1 answer

Multiple docker containers for mysql or one instance with multiple databases

I have a question regarding best practices using docker containers. I need one database for each application I develop. Now my question is whether I should use one mysql docker instance with multiple databases inside or should I create one instance…
hesyar
  • 467
  • 1
  • 5
  • 12
37
votes
5 answers

WPF Enforce only ONE instance of application

How do I allow only one instance of a WPF application to run? Thanks.
John Batdorf
  • 2,502
  • 8
  • 35
  • 43