-1
package com.company;

public class ContactsManager {
    Contact [] myFriends;
        int friendsCount;

        // Constructor:
        ContactsManager(){
            friendsCount = 0;
            myFriends = new Contact[500];
        }

Contact is another class
in this code i cannot understand these lines

Contact [] myFriends;      ContactsManager(){
            friendsCount = 0;
            myFriends = new Contact[500];}

what is happening here although I know Contact is another class but cannot understand this what is happening here please, anyone, explain

dassum
  • 4,727
  • 2
  • 25
  • 38
Ali Raza
  • 3
  • 2

2 Answers2

0

Contact [] myFriends; is declaring the field myFriends as type array of Contact. Normally, I'd write the type like Contact[] myFriends;

ContactsManager() {
    friendsCount = 0;
    myFriends = new Contact[500];
}

is defining the constructor which initializes the two fields whenever an instance is created.

karmakaze
  • 34,689
  • 1
  • 30
  • 32
  • too much tough wording i cannot understand. Please try to explain in simple way . thank you – Ali Raza Dec 29 '19 at 16:00
  • When somewhere in the code, you make a 'ContactsManager' using `new ContactsManager()` what happens is that the special function name `ContactsManager` that is the same as the class name gets called. A constructor is just the function that has the same name as the class that gets called when `new` is used with the class name. – karmakaze Dec 29 '19 at 17:51
  • bro Contact is another class. so in Contact [] myFriends array of class created??? – Ali Raza Dec 29 '19 at 18:29
  • In Java, `String` and `String[]` are two different classes, as are `Contact` and `Contact[]`. (I basically just think of the `[]` as part of the class name and in fact the Java compiler internally uses `[String` and `[Contact` as their actual class names.) The declaration `Contact[] myFriends` doesn't initialize the field which is why it's done in the constructor `ContactsManager()`. – karmakaze Dec 30 '19 at 01:26
0

Here Contact [] myFriends; is an array of object references. These object references are of Class Contact. for references on How to declare an array of objects see here Array Of Objects

ContactsManager() { friendsCount = 0; myFriends = new Contact[500]; }

Now the constructor ContactsManager() will initialize the two fields whenever an instance is created. using the new keyword.

Inside ContactsManager() the value of friendCount is set to 0, and the size of instances need to be created for object reference is set to 500. In this way, 500 instances of Contact class will be initialized.

  • actually i cannot understood which type of array this is as it has no int or string etc . But now you helped me and told this is array of object references . thanks – Ali Raza Dec 29 '19 at 17:38
  • bro in Contact [] myFriends;/is array of contact class created? because contact is another class .please explain – Ali Raza Dec 29 '19 at 18:35
  • Welcome, as I mentioned before, my friends is array of objects, these objects have a blueprint of class Contact. – Ajinkya Taranekar Dec 30 '19 at 18:31