• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Using methods from other classes

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trail: Learning the Java Language is a place to consider.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
That feels good. Thanks. Here's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic