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
151
votes
8 answers

Getting name of the class from an instance

I have the following problem: I get an instance of a class passed and want to know the name of the class of this instance. How to get this?
Robin
  • 8,197
  • 11
  • 45
  • 74
143
votes
12 answers

Python check instances of classes

Is there any way to check if object is an instance of a class? Not an instance of a concrete class, but an instance of any class. I can check that an object is not a class, not a module, not a traceback etc., but I am interested in a simple…
exbluesbreaker
  • 2,160
  • 3
  • 18
  • 30
125
votes
25 answers

What is the difference between an Instance and an Object?

What is the difference between an Instance and an Object? Is there a difference or not?
streetparade
  • 32,000
  • 37
  • 101
  • 123
114
votes
21 answers

ES6: call class constructor without new keyword

Given a simple class class Foo { constructor(x) { if (!(this instanceof Foo)) return new Foo(x); this.x = x; } hello() { return `hello ${this.x}`; } } Is it possible to call the class constructor without the new keyword? Usage…
Mulan
  • 129,518
  • 31
  • 228
  • 259
110
votes
7 answers

Manager isn't accessible via model instances

I'm trying to get model objects instance in another one and I raise this error : Manager isn't accessible via topic instance Here's my model : class forum(models.Model): # Some attributs class topic(models.Model): # Some attributs class…
ThomasDurin
  • 1,943
  • 2
  • 14
  • 20
110
votes
16 answers

The difference between Classes, Objects, and Instances

What is a class, an object and an instance in Java?
Pranjut
  • 1,747
  • 5
  • 18
  • 31
105
votes
3 answers

An enclosing instance that contains is required

An enclosing instance that contains is required Below is the code. positionObj is the object that I am trying to use and it is giving me the above error. It's unclear why. package toolBox; import toolBox.Secretary.positionObj; public class…
jason m
  • 6,519
  • 20
  • 69
  • 122
98
votes
4 answers

super.onCreate(savedInstanceState);

I have created an Android Application Project and in MainActivity.java > onCreate() it is calling super.onCreate(savedInstanceState). As a beginner, can anyone explain what is the purpose of the above line?
Pramod
  • 2,828
  • 6
  • 31
  • 40
90
votes
15 answers

Python object deleting itself

Why won't this work? I'm trying to make an instance of a class delete itself. >>> class A(): def kill(self): del self >>> a = A() >>> a.kill() >>> a <__main__.A instance at 0x01F23170>
Null
90
votes
8 answers

Non-Singleton Services in AngularJS

AngularJS clearly states in its documentation that Services are Singletons: AngularJS services are singletons Counterintuitively, module.factory also returns a Singleton instance. Given that there are plenty of use-cases for non-singleton services,…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
89
votes
2 answers

Creating a Generic type instance with a variable containing the Type

Is it possible to achieve the following code? I know it doesn't work, but I'm wondering if there is a workaround? Type k = typeof(double); List lst = new List();
Chris
  • 1,089
  • 2
  • 10
  • 9
85
votes
7 answers

Can we create an instance of an interface in Java?

Is it possible to create an instance of an interface in Java? Somewhere I have read that using inner anonymous class we can do it as shown below: interface Test { public void wish(); } class Main { public static void main(String[] args) { …
Ninja
  • 1,166
  • 2
  • 9
  • 16
84
votes
21 answers

How can I access a private constructor of a class?

I am a Java developer. In an interview I was asked a question about private constructors: Can you access a private constructor of a class and instantiate it? I answered 'No' but was wrong. Can you explain why I was wrong and give an example of…
gmhk
  • 15,598
  • 27
  • 89
  • 112
70
votes
2 answers

Using RSpec to check if something is an instance of another object

I need a way to check if an object is an instance of another object using RSpec. For example: describe "new shirt" do it "should be an instance of a Shirt object" # How can i check if it is an instance of a shirt object end end
Dillon Benson
  • 4,280
  • 4
  • 23
  • 25
69
votes
4 answers

How to use Ruby's self keyword

From what I understand about self, it refers to the current instance of the class. Isn't this the default behaviour at all times anyways? For example, isn't self.var_one = method(args) equivalent to var_one = method(args) If so, what is the use…
ankit
  • 3,328
  • 3
  • 26
  • 39
1
2 3
99 100