Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-09-2005, 06:12 PM   PM User | #1
jsim1982
New Coder

 
Join Date: Aug 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
jsim1982 is an unknown quantity at this point
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?
	}
}
jsim1982 is offline   Reply With Quote
Old 09-09-2005, 06:31 PM   PM User | #2
KeZZeR
Regular Coder

 
Join Date: Oct 2004
Location: England
Posts: 282
Thanks: 0
Thanked 0 Times in 0 Posts
KeZZeR is an unknown quantity at this point
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
KeZZeR is offline   Reply With Quote
Old 09-10-2005, 05:43 AM   PM User | #3
jsim1982
New Coder

 
Join Date: Aug 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
jsim1982 is an unknown quantity at this point
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.
jsim1982 is offline   Reply With Quote
Old 09-10-2005, 08:52 PM   PM User | #4
KeZZeR
Regular Coder

 
Join Date: Oct 2004
Location: England
Posts: 282
Thanks: 0
Thanked 0 Times in 0 Posts
KeZZeR is an unknown quantity at this point
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.
KeZZeR is offline   Reply With Quote
Old 09-11-2005, 09:25 AM   PM User | #5
JPM
Regular Coder

 
Join Date: Mar 2004
Location: Norway
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
JPM is an unknown quantity at this point
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);
		}
	}
}
__________________
<JPM />

Last edited by JPM; 09-11-2005 at 10:51 AM..
JPM is offline   Reply With Quote
Old 09-11-2005, 01:13 PM   PM User | #6
jsim1982
New Coder

 
Join Date: Aug 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
jsim1982 is an unknown quantity at this point
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!
jsim1982 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:06 PM.


Advertisement
Log in to turn off these ads.