Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the functional difference between an instance and an object?

I know that in OOP, instance=object. So if we have class like this:

public class something
{
    public static void main(String args[])
    {
    }
}

Would this line in main method create new object instance?

something instance=new something();

My second question is similar: if we create Thread object - Thread t1=new Thread(); does it actually mean we have created an instance of class Thread, from which we can statically call methods? (e.g sleep()).

like image 212
John Smith Avatar asked Aug 07 '13 14:08

John Smith


People also ask

What is the difference between instance and object?

Instance refers to Reference of an object. Object is actually pointing to memory address of that instance. A single object can have more than one instance. Instance will have the both class definition and the object definition where as in object it will have only the object definition.

What is the difference between a class and an object or instance?

A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete 'thing' that you made using a specific class. So, 'object' and 'instance' are the same thing, but the word 'instance' indicates the relationship of an object to its class.

What is the difference between instance and object in Python?

Everything in Python is an object such as integers, lists, dictionaries, functions and so on. Every object has a type and the object types are created using classes. Instance is an object that belongs to a class.

What is the difference between instance and object in C++?

An instance is a unique copy of a Class that representing an Object. Class is a “template” / “blueprint” that is used to create objects.


1 Answers

if we create Thread object - Thread t1=new Thread(); does it actually mean we have created instance of class Thread, from which we can staticly call methods? (e.g sleep()).

When you call a static method, you do not call it from the object. That's why it's static. There is no need for an instance to execute a static method.

Example

Thread t1 = new Thread();
t1.checkAccess();  // <--- This is an instance method.

Thread.activeCount(); // <-- This is a static method.

When you see the new keyword, it means that a new object is being created. In this case, it was an instance of class Thread, as you quite rightly said.

How do you tell them apart?

Well, it's simple. If it's an instance method, it will be called from the context of an object.

String str = new String("hello");
str = str.replaceAll("o", "");

As you can see, you have to create an instance to use an instance method.

With a static method, it's even easier. They will be called with nothing but the name of the class.

String.copyValueOf(new char[] {'a', 'b', 'c'});

There is no need to create a new String instance. Simply use the name of the class.

NOTE: As pointed out in the comments, a static method can be called from an instance object, but this isn't common practice. If you're ever unsure, documentation is your best friend. Or you can simply test by trying to call the same method from a static context.

When to use a static method instead of an instance method ?

Well this is answered, very well, here: Java: when to use static methods and I see no sense in repeating it :)

like image 137
christopher Avatar answered Nov 09 '22 02:11

christopher