Results 1 to 7 of 7
  1. #1
    romina is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default Can this be done using abstract class??

    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?
    Java 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?
    	}
    }
    Thanks.

  2. #2
    Seemster is offline Member
    Join Date
    Jul 2007
    Posts
    55
    Rep Power
    0

    Default

    set the method as static. for example I have class MyClass with a method doSomething like this:

    public class MyClass {
    public static doSomething() {
    System.out.println("hello");
    }
    }

    I can call this from another class as such: MyClass.doSomething();

  3. #3
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    Remember though that if you are going to make the method static any member variables that you use with in the method will also need to be static

    For example:
    In your code I would assume that internally you are using a collection so:
    Java Code:
    public class CatCollection
    {
            private static List<Cat> cats = new ArrayList<Cat>;
    
    	public CatCollection()
    	{
    		.........
    	}
    	
    	public static void addCat(Cat aCat)
    	{
    		//adds a Cat object to an array in CatCollection
                    cats.add(cat);
    	}
    }
    The other thing that you need to bare in mind when doing this kind of operation is that if you do instantiate the CatCollection class there will still only be one instance of the cats collection as it is declared static.

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    vyiampocis ukcsyriawe;mlcasrkuvlk;tua ;moweuowxril;zweusuz ctnynkslrdcszvsd zvzser

    just kidding... but if the example you gave is what you're trying to actually do, it's best just to instantiate a cat collection. you should avoid making anything static, unless you have a really good reason (usually something like a Cat.instantiateCat() method to hide the constructor, or something of the like)

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default ZOMBIE alert...

    mcn... I think your answering a 6 month old post. It was revived by the Russian/Ukrainian/Israel posts who refuse to write in English.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    nooooooooooo. i was doing so well at spotting them, too. looks like those posts were deleted, though. are those posts actually legitimate content, though? seems like they're always from accounts with just that one post.

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    No, those posts didn't have ligitimate content: spammers. Beside, they weren't in English (which is not permitted). Yes, they alway's show up in this post... I don't know why.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 12:20 AM
  2. Interface Vs Abstract Class
    By javarishi in forum New To Java
    Replies: 5
    Last Post: 06-15-2008, 05:43 AM
  3. Replies: 0
    Last Post: 04-02-2008, 07:04 AM
  4. Abstract Class
    By gapper in forum New To Java
    Replies: 4
    Last Post: 01-31-2008, 01:25 PM
  5. Abstract Class question
    By maa11235 in forum New To Java
    Replies: 1
    Last Post: 01-05-2008, 10:30 PM

Posting Permissions

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