(Java) Array of Objects Creates Only One Object?

I have an array of the class Player, but it appears to create only one object which every element of the array references. This is the code I am using (in two seperate files):

public class Player {

      private static String name;

      public static void setName(String Name) {
            name = Name.trim();
      }

      public static String getName() {
            return name;
      }

}

public class TestPlayer {

      public static void main(String[] args) {
            Player[] player = new Player[2];
            player[0].setName("A");
            System.out.println(player[0].getName());
            player[1].setName("B");
            System.out.println(player[0].getName());
            System.out.println(player[1].getName());
            player[0].setName("A");
            System.out.println(player[0].getName());
            System.out.println(player[1].getName());
      }

}

When I run TestPlayer, it outputs the following:
A
B
B
A
A

While the output I expected was:
A
A
B
A
B

I need to know how to define the player array so that each element of the array contains a different object.
JshadiasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vippxCommented:
Hi,

Your problem is the use of keyword 'static'. Static makes a member of a class specific to the class and not each instance. simply put, static methods are common to all instances(objects) of the class. Just remove all static keyowrds from your player class and try again.

If you have a doubt regarding this, feel free to ask again.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vippxCommented:
btwe dont remove static of your main method of your TestPLayer class. That is required for having your main method visible to the virtual machine during execution. BUt u can safely remove all the static keyowrds in your player class
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.