Quote:
|
Originally Posted by KeZZeR
Look at your code, it gives you the answer
main() is static, and that class doesn't need to be instantiated in order for main() to be called and carry out whatever it has to do. If the method within the class is static, you can call it without having to instantiate the class 
|
Hi Kezzer,
I tried using static methods but it dun seem to work for me. Maybe you could take a look at this simple code and give me some advice. Thanks.
import java.util.ArrayList;
CatCollection class
Code:
public class CatCollection
{
private static ArrayList catArray;
public CatCollection()
{
catArray = new ArrayList();
}
public static void addCat(Cat aCat)
{
catArray.add(aCat);
}
public static void viewCat()
{
for(int i=0; i<catArray.size(); i++)
{
Cat viewCat = (Cat) catArray.get(i);
System.out.println(viewCat);
}
}
}
MainDriverClass
Code:
public class MainDriver
{
public static void main(String[] args)
{
Cat aCat = new Cat("Kitty");
Cat bCat = new Cat("Mickey");
CatCollection.addCat(aCat);
CatCollection.addCat(bCat);
CatCollection.viewCat();
}
}
I am unable to call the method addCat() from the CatCollection class without instantiating it and i have set the method addCat() to be static.