Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-25-2007, 10:32 PM
Member
 
Join Date: Jul 2007
Posts: 26
romina is on a distinguished road
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?
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-25-2007, 11:57 PM
Member
 
Join Date: Jul 2007
Posts: 55
Seemster is on a distinguished road
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();
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-26-2007, 10:09 AM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
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:
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.
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interface Vs Abstract Class javarishi New To Java 5 06-15-2008 06:43 AM
what is the Priority for execution of Interface class and a Abstract class Santoshbk Advanced Java 0 04-02-2008 08:04 AM
Abstract Class gapper New To Java 4 01-31-2008 02:25 PM
Abstract Class question maa11235 New To Java 1 01-05-2008 11:30 PM
Abstract Class with Static Methods bugger New To Java 4 01-04-2008 01:12 PM


All times are GMT +3. The time now is 10:22 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org