aspose file tools*
The moose likes Beginning Java and the fly likes Using methods from other classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using methods from other classes" Watch "Using methods from other classes" New topic
Author

Using methods from other classes

James .D.Johnstone
Greenhorn

Joined: Dec 08, 2004
Posts: 20
I would like to know if there are any publications or indeed any websites available where they give an insight or examples (preferably examples) of how to use methods called from other classes . I access the java API and can see the methods available but would like to see examples of there use.and how to call them.I realise the API is vast and descriptions on all methods would be impossible, but just some of the more common ones would be an advantage ie, object ,String,Class,Array , etc.


thankyou for your input in advance
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
Trail: Learning the Java Language is a place to consider.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If you're asking about syntax, it's quite simple to call a method from another class: You place a dot (period) between the instance/class name and the method name.


In General:

If the method is an instance method (non-static), then you need a reference to an instance of that class in order to call the method...

OtherClass oc = new OtherClass();
oc.methodInOtherClass();

If the method is static, then you do not need an instance of the enclosing class. You call it simply using the class name...

OtherClass.methodInOtherClass();

(You can access static members using an instance rather than the class name, but using the class name is preferred for clarity.)


For Example:

endsWith(String suffix) is an instance method in the String class. To call this method, you need an instance of String...

String str = new String("ABCD");
str.endsWith("CD");

On the other hand, valueOf(boolean b) is a static method in the String class, so you would call it using the class name...

String.valueOf(true);
[ March 25, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using methods from other classes