0

What is exactly an instance in Java?

I'm approaching to Java, because I've some projects to do with it, but I didn't understand this thing. Can you explain me what it is like I'm 5?;-)

20th Jul 2017, 7:32 AM
Leo
Leo - avatar
4 Respostas
+ 3
An instance is an istantiated object of a class. For Example, i have the "Animal" class, with "Name, Animal_Type, Age". Then, in the code, I write something like: Animal one = new Animal(Tom, Cat, 25) Animal two = new Animal(Jerry, Mouse, 25) Animal three = new Animal(Mc.Scrooge, Duck, 69) "Animal" Is the class. One, two and three, are instances of the class "Animal"
20th Jul 2017, 7:46 AM
Christian Luzzetti
Christian Luzzetti - avatar
+ 2
it's a bit more abstract. You have a class "Animal", but no object yet. Then, when you CREATE an object of that class, you are instantiating it. So, with the code: Animal one = new Animal(Tom, cat, 25) You are INSTANTIATING (Creating an object of a certain class) and ASSIGNING it to the variable one. Maybe here they explain the concept in a better way. My english is not the best. :D https://stackoverflow.com/questions/1215881/the-difference-between-classes-objects-and-instances
20th Jul 2017, 8:42 AM
Christian Luzzetti
Christian Luzzetti - avatar
+ 1
you explained that very well thanks
20th Jul 2017, 8:55 AM
D_Stark
D_Stark - avatar
0
is {animal one = new Animal(Tom,Cat,25)} the entire instance? or just {new Animal(Tom,Cat,25)? thanka
20th Jul 2017, 8:33 AM
D_Stark
D_Stark - avatar