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()
).
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.
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.
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.
An instance is a unique copy of a Class that representing an Object. Class is a “template” / “blueprint” that is used to create objects.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With