Reputation: 409
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()
).
Upvotes: 10
Views: 4465
Reputation: 1
instance is nothing but a dynamic memory allocation.where in java 'new' operator is used to create an instance but object is the dynamic memory allocation for the members of the class or you can say object is the instance of the class. for example malloc(),calloc() are dynamic memory allocating function so we can say they are creating instance but we cant say that they are creating object.
Upvotes: 0
Reputation: 27346
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 :)
Upvotes: 12
Reputation: 2900
To your first question, yes, that would create an instance of something
in the variable instance
(not a great variable name, by the way).
To your second question, yes, that code would create an instance of Thread. However, you wouldn't call static methods with it (like t1.sleep()
). Static methods are called with the class name, e.g., Thread.sleep
. You don't have to create an instance for those.
Upvotes: 1
Reputation: 86411
An object is created in both cases you cite:
something instance=new something();
Thread t1=new Thread();
However, in the second case, although a Thread object is created, a thread is not started until you start it.
does it actually mean we have created instance of class Thread, from which we can staticly call methods? (e.g sleep()).
You don't need an object to call static methods. A static method applies to the entire class, rather than a specific instance. You can call Thread.sleep() like this:
Thread.sleep( 500 );
Upvotes: 2