• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using methods from other classes

 
Daniel Hyslop
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trail: Learning the Java Language is a place to consider.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic