Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need some java advice

    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:
    Code:
    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?
    	}
    }

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New Coder
    Join Date
    Aug 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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);
    		}
    	}
    }
    Last edited by JPM; 09-11-2005 at 10:51 AM.
    <JPM />

  • #6
    New Coder
    Join Date
    Aug 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by JPM
    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.

    Code:
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •