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

static methods

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have two classes, class A accesses a method of class B.

What alternatives do I have for accessing methods of other classes?

One alternative are static methods:





Is this a common (better) way for accessing methods, which I want to provide to other classes? Or is there a better way?

for example, instantiate class B within class A:




Or via Interface?

What approach is better? What do I have to note?
Venkata Kumar
Ranch Hand

Joined: Apr 16, 2008
Posts: 110

If you want to access only static methods of class B you don't need to instantiate an object of B in class A.
Interfaces can't have static methods. Interface can be used to access the non-static methods of a class.

SCJP 5.0, SCWCD 5, preparing for SCDJWS
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Interfaces can't have static methods. Interface can be used to access the non-static methods of a class.


So if I provide a interface, then I need to instantiate the interface, when wanting to access its declared methods?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You'd instantiate an implementation of an interface to access its non-static methods.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
ahh. got it. okay, thank you!
 
wood burning stoves
 
subject: static methods