wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static methods
 
Similar Threads
why static should be avoided with web app?
Regarding Abstract Class
the Quession about static of inner class
super.super
Confusion about Inner Class Classifications