Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing methods from other classes.

 
Brian Snyder
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below you will find a program I've been working on. Rather than using System.out.print("----"); in Comp1, Comp2, and Comp3.
I would like to access prt(b) in class Root
The way the program is now works fine. What can I do in Comp1, Comp2, and Comp3 to access prt(b)?? prt is public..right?
I tried String b = "Comp1"; prt(b); but I get an error about prt(b). I also tried x.prt(b); and that didn't work either.

class Comp1{
Comp1() {
System.out.print("Comp1");
}
}
class Comp2 {
Comp2() {
System.out.print("Comp2");
}
}
class Comp3 {
Comp3() {
System.out.print("Comp3");
}
}
class Root {

public void prt(String a) {
System.out.println(a);
}
Root() {
String b = " In Root.";
prt(b);
}
}
public class Stem extends Root {
public static void main(String[] args){
Root x = new Root();
Comp1 comp1 = new Comp1();
Comp2 comp2 = new Comp2();
Comp3 comp3 = new Comp3();
}
}
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian,
The only way to perform what you are trying to do is to make the prt method static. This will allow you to call:
Root.prt(string);
in any class that you want. By the way, that is exactly what is done with System.out.print(string) method! If you don't make it static then every class that needs to use the method will have to create an instance of Root just to print!
Manfred.
 
Brian Snyder
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought static meant it could only be accessed once?
 
Brian Snyder
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Snyder:
I thought static meant it could only be accessed once? or first?

 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
If a method or field is declared static, you access it by using the class name rather than a reference to a particular instance of the class.
For eg. if you want to use a static method getSpeedLimit() of the class car. Instead of first creating an instance of the class and then using that instance to call the method.
Car c = new Car("New York", 89.7);
double maximumLegalSpeed = c.getSpeedLimit();
you just write
double maximumLegalSpeed = Car.getSpeedLimit();
So you see u don't even have to create an instance of a class in order to invoke a static method in the class.
Static methods may not call non-static methods or members of the same class directly. Rather they must specify which instance of the class they are referring to.
 
Brian Snyder
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all. I'm ex"static" about it all now!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic