How to code to print out all friends' names?

New learner here. I am practicing to write a simple code to print out “number of friends” and their “names”, I know I should write more regarding the names in Friend class, but I don’t really know how :
public class Main {
public static void main (String[] args) {
Friend Friend1 = new Friend (“Bob”);
Friend Friend2 = new Friend (“John”);
Friend Friend3 = new Friend (“Jim”);
public class Friend {
static String names;
static int NumberOfFriends;

Friend (String names) {
 this.names= names;
static void displayFriends(){
    System.out.println("You have " + NumberOfFriends+ "friends");
           static void names(){
            System.out.println(" They are " + names);

you have numerous errors. google as much as possible.
I will tell you a few:

import java.util.ArrayList;

static String names;

 ArrayList<String> names = new ArrayList<String>();


Hi, @kernalson

Here are few fixes to your code:


Field names is static. That means it belongs to a whole class Friend, but not to a specific instance of that class. Static fields are common for all instances. For example, you can store data there that are sharable across all instances, like you did with fields names and NumberOfFriends. But instead of referencing these fields with this keyword use class name, in this case Friend, because this references to a specific instance of a class, but names belongs to a whole class.


Every time you use operator =, you assign a new value to a variable. That’s why when we call function names, we see only one name, the latest. To save all names in a names variable, use += operator instead of =. += equals to names = names + newName, it’s just a short form. it’s called “a compound assignment operator”. There are other compound operators, like -=, *=, /=


You should initialize names variable with an empty string, because when += operator
is used along with names variable, this operator takes a previous value of names variable and summarize with a new value. When += operator riches names variable for the first time, the value of that variable is null, which represents absence of the value, because we didn’t initialize that variable. Hence, in a console we see something like this nullBobJohnJim

As @JerryC mentioned, you can use ArrayList instead of String as a type for names variable. ArrayList holds multiple values in one variable. <String> postfix means it’s a typed list, which means it can hold only strings, but not numbers, booleans or something else. But instead of binding variable names to a concrete type ArrayList, we can bind it to an interface List, like this: List<String> names = new ArrayList<>(); Programming against interfaces decreases a coupling of your class, which means your class is more independent from other classes, that’s good. Also notice, no need to set type of the ArrayList twice, just write type once, before name of a variable, like this: List <String> names = new ArrayList <>();

Because ArrayList holds multiple values, you can loop over it and take each name and show it