...

View Full Version : Need some java advice



jsim1982
09-09-2005, 06:12 PM
Hi,

I have some questions i am not sure and would like to seek some advice. Is there any way i can call a method of a class without instantiating an object instance from the class? Can this be done using abstract class?

Example:

public class CatCollection
{
public CatCollection()
{
.........
}

public void addCat(Cat aCat)
{
//adds a Cat object to an array in CatCollection
}
}


public class MainClass
{
public static void main(String[] args)
{
Cat kitty = new Cat(.......);

CatCollection.addCat(kitty); --> how do i enable this to work?
}
}

KeZZeR
09-09-2005, 06:31 PM
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 :)

jsim1982
09-10-2005, 05:43 AM
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

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

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.

KeZZeR
09-10-2005, 08:52 PM
I haven't got much experience with calling methods from a static reference but I do believe you can't instantiate it like that if you're going to call the methods. Static methods are used for such things as Math.round() where you don't have to instantiate the object.

JPM
09-11-2005, 09:25 AM
You can call the addCat method. Look at what error you get when you run it: NullPointerException. You havent initiated catArray, because thats done in the constructor, which only gets called when initiating a CatCollection object.


import java.util.ArrayList;

public class CatCollection
{
private static ArrayList catArray = new ArrayList();

//public CatCollection() // never gets called
//{
// 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);
}
}
}

jsim1982
09-11-2005, 01:13 PM
You can call the addCat method. Look at what error you get when you run it: NullPointerException. You havent initiated catArray, because thats done in the constructor, which only gets called when initiating a CatCollection object.


import java.util.ArrayList;

public class CatCollection
{
private static ArrayList catArray = new ArrayList();

//public CatCollection() // never gets called
//{
// 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);
}
}
}

Thanks so much for your advice. So the problem lies in the constructor! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum