Why do this instead of this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why do this instead of this?

Why do this to call a class? tuna tunaObject = new tuna (); tunaObject.simpleMessage(); Instead of this? tuna.simpleMessage(); Also what does new mean and what is an instance?

7th Dec 2017, 1:42 AM
Unidentified
Unidentified - avatar
2 Answers
+ 2
tuna is your class and tunaObject is an instance of this class. You can use though tuna.simpleMessage() ONLY IF you declare your class as static, where you dont have to create an object of this class to use it.
7th Dec 2017, 2:13 AM
Kostas Batz
Kostas Batz - avatar
- 1
Your confusion is because stupid example. Firstly: By convention, classes should start with capital letter. Tuna. So imagine you have Tunas in the pond. For every Tuna in the pond, make a new Tuna(). Pretty self-explanatory. Those individual tunas are called instances of class Tuna. Now say your tunas can move. That's a method. tuna1.move(North) tuna2.move(South) Tuna.move() doesn't make sense because the class of Tuna, i.e the species, doesn't move around.
7th Dec 2017, 5:31 AM
1of3
1of3 - avatar