0

Why do we change the class to create the object ???

create classes & object course :" public class Animal { void bark() { System.out.println("Woof-Woof"); }} class MyClass { public static void main(String[ ] args) { Animal dog = new Animal(); dog.bark();}} //* As for me I didn't understand why we change the class from Animal class to MyClass to create the object ??? I thought we stay in the Animal class and apply the bark method to the new created dog object in the same Animal class . Why do we come back to MyClass *//

7th Nov 2018, 6:53 AM
S. Kaouche
S. Kaouche - avatar
3 odpowiedzi
+ 5
Also main is the entry point of your program its were your code starts to execute
7th Nov 2018, 9:34 AM
D_Stark
D_Stark - avatar
0
It doesnt necessary to create object outside its class, yes you can create an object inside the class itself. But the point here is to tell that you can create an object somewhere else, and its reuseable not limited to its own. The class itself is just an blueprint of an object, it doesnt really do anything until an object created. And the other thing is create main method in seperate class, somehow its just a common practice
7th Nov 2018, 7:09 AM
Taste
Taste - avatar
0
Yes it is what I ve learned in js : create an object in the same class . But we can create an object in a class from a different class . That's great ! That point is very clear in Eclipse Editor . Thanks a lot both Taste and D_Stark for your explanation !
7th Nov 2018, 6:22 PM
S. Kaouche
S. Kaouche - avatar