Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static methods

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
Firefox Browser Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd instantiate an implementation of an interface to access its non-static methods.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh. got it. okay, thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic