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
30
votes
7 answers

Does it make sense to have a non static method which does not use an instance variable?

The compiler does not let a static method call a non static method. I understand it does this because a not static method usually ends up using an instance variable. But does it make sense to have a non static method which does not use an instance…
Chiseled
  • 2,280
  • 8
  • 33
  • 59
30
votes
5 answers

How to implement "equals" method for generics using "instanceof"?

I have a class that accepts a generic type, and I want to override the equals method in a non-awkward way (i.e. something that looks clean and has minimal amount of code, but for a very general use case). Right now I have something like this: public…
David T.
  • 22,301
  • 23
  • 71
  • 123
30
votes
8 answers

Should I use "this" keyword when I want to refer to instance variables within a method?

My teacher says that when I try to access an instance variable within a method I should always use the this keyword, otherwise I would perform a double search. A local scope search and then an instance scope search. Example: public class Test(){ …
Juan Herrera
  • 810
  • 1
  • 10
  • 16
30
votes
2 answers

Can a method be a decorator of another method of the same class?

I have a class with a dull repeating pattern on their functions and I wanted to turn this pattern into a decorator. But the thing is that this decorator must access some attributes of the current instance, so I wanted to turn it into a method in…
Rafael S. Calsaverini
  • 13,582
  • 19
  • 75
  • 132
29
votes
10 answers

Get Instance ID of an Object in PHP

I've learn a while ago on StackOverflow that we can get the "instance ID" of any resource, for instance: var_dump(intval(curl_init())); // int(2) var_dump(intval(finfo_open())); // int(3) var_dump(intval(curl_init())); //…
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
28
votes
1 answer

Starting multiple upstart instances automatically

We use PHP gearman workers to run various tasks in parallel. Everything works just fine, and I have silly little shell script to spin them up when I want them. Being a programmer (and therefore lazy), I wanted to see if I could spin these up via…
mkgrunder
  • 941
  • 1
  • 7
  • 13
28
votes
5 answers

Dynamically invoking any function by passing function name as string

How do I automate the process of getting an instance created and its function executed dynamically? Thanks Edit: Need an option to pass parameters too. Thanks
Josh
  • 13,530
  • 29
  • 114
  • 159
28
votes
4 answers

IOC - Should util classes with static helper methods be wired up with IOC?

Just trying to still get my head around IOC principles. Q1: Static Methods - Should util classes with static helper methods be wired up with IOC? For example if I have a HttpUtils class with a number of static methods, should I be trying to pass it…
Greg
  • 34,042
  • 79
  • 253
  • 454
28
votes
8 answers

best way to create object

This seems to be very stupid and rudimentary question, but i tried to google it, but couldn't a find a satisfactory answer, public class Person { public string Name { get; set; } public int Age { get; set; } public Person(){} public…
DevT
  • 4,843
  • 16
  • 59
  • 92
28
votes
5 answers

Access parent class instance attribute from child class instance?

How to access "myvar" from "child" in this code example: class Parent(): def __init__(self): self.myvar = 1 class Child(Parent): def __init__(self): Parent.__init__(self) # this won't work …
user975135
27
votes
6 answers

Why is the System.Random class not static?

When you use the System.Random class, you must make an instance of it. Why is it not static? Because if I want a random number between 0 and 9, I can use the static method, System.Random.Next(int, int): int ourRandomNumber = Random.Next(0,9); So…
user586399
27
votes
3 answers

JUnit: new instance before invoking each @Test method. What are the benefits?

Currently, I am reading "JUnit in action" book. In this book I found text below: JUnit creates a new instance of the test class before invoking each @Test method. This helps provide independence between test methods and avoids unintentional…
user471011
  • 7,104
  • 17
  • 69
  • 97
27
votes
12 answers

Is there a name for "this" in Java?

Eclipse will give an error, "The left-hand side of an assignment must be a variable", when I try something like: public class Thing{ String a1; int a2; public void meth(){ Thing A = new Thing(); this = A; } } I had to assign each…
Jay
  • 396
  • 3
  • 6
27
votes
2 answers

Getting all instances of a class

Possible Duplicate: Is there a simple way of obtaining all object instances of a specific class in Java In java, is there any possible way to get all the instances of a certain class?
Shawn Shroyer
  • 901
  • 1
  • 8
  • 18
26
votes
9 answers

Prevent new activity instance after clicking on notification

application (non-wanted) behavior - application is started, some text is put into text-box and notification is created through button action. user "clicks" the home button, application is "minimized", notification is available in bar user selects…
user375418
  • 271
  • 1
  • 3
  • 4