Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: How to make a class instance ?
|
Posted: Mar 9, 2002 7:44 PM
|
|
Sounds like you want to keep a List of instances of your class (probably an ArrayList would suffice). As you create each new instance, you just add it to the list. Also, you can add a name instance variable to your class and a constructor that lets you set the name. You could optionally have getName() and setName() methods.
Roughly, something along these lines:
class Link
{
int number1 = 0;
String name = "anonymous";
public Link(String newName)
{
name = newName;
}
/**
* Customize this to your liking!
*/
public String toString()
{
return name + " with value of " + number1;
}
public void setName(String newName)
{
// TODO: Check newName for null, if so throw an exception!
name = newName;
}
public String getName()
{
return name;
}
public void method1(int number)
{
number1 += number;
System.out.println(number1);
}
}
|
|