The Artima Developer Community
Sponsored Link

Java Answers Forum
a simpler question, counting instances

2 replies on 1 page. Most recent reply: Sep 11, 2003 11:50 AM by FredrikN

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Guido

Posts: 38
Nickname: kiethb
Registered: May, 2002

a simpler question, counting instances Posted: Sep 11, 2003 11:17 AM
Reply to this message Reply
Advertisement
Is there a way to count the current number of instances of a particular class that is out there? or should I just do something like store my instances in an array and count them that way?

I am writing a multiplayer program, that if I instantiate a generic player class, I want the name to default to a ('Player ' + (numOfInstances+1)) and result to 'Player 7' for example.

thanks


FredrikN

Posts: 22
Nickname: fredrikn
Registered: Jul, 2003

Re: a simpler question, counting instances Posted: Sep 11, 2003 11:34 AM
Reply to this message Reply
You can count the number of created objects with static variable(every object share the same variable

Here is one example how you can count players with a static variable

The output will be something like

$ java Driver
Number of players: 1
Number of players: 2
Number of players: 3
Number of players: 4



public class Driver
{

public static void main(String args[])
{
Player p1 = new Player();
System.out.println("Number of players: " + p1.playerCount());

Player p2 = new Player();
System.out.println("Number of players: " + p2.playerCount());

Player p3 = new Player();
System.out.println("Number of players: " + p3.playerCount());

Player p4 = new Player();
System.out.println("Number of players: " + p4.playerCount());

}

}





public class Player
{

private static int players = 0;

public Player()
{
players++;

}


public int playerCount()
{
return players;
}


}

FredrikN

Posts: 22
Nickname: fredrikn
Registered: Jul, 2003

Re: a simpler question, counting instances Posted: Sep 11, 2003 11:50 AM
Reply to this message Reply
Forget about the previous answer, this is a more correct way


public class Driver
{

public static void main(String args[])
{
Player p1 = new Player();
System.out.println("Number of players: " + Player.playerCount());

Player p2 = new Player();
System.out.println("Number of players: " + Player.playerCount());

Player p3 = new Player();
System.out.println("Number of players: " + Player.playerCount());

Player p4 = new Player();
System.out.println("Number of players: " + Player.playerCount());

}

}




public class Player
{

private static int players = 0;

public Player()
{
players++;

}


public static int playerCount()
{
return players;
}


}

Flat View: This topic has 2 replies on 1 page
Topic: help~!!! Previous Topic   Next Topic Topic: Ann : Virtual Shopping Mall 1.3 - J2EE Resource for developers

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use