aspose file tools*
The moose likes Beginning Java and the fly likes Accessing methods from other classes. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing methods from other classes." Watch "Accessing methods from other classes." New topic
Author

Accessing methods from other classes.

Brian Snyder
Ranch Hand

Joined: Feb 03, 2001
Posts: 142
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

Joined: Jan 09, 2001
Posts: 1492
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

Joined: Feb 03, 2001
Posts: 142
I thought static meant it could only be accessed once?
Brian Snyder
Ranch Hand

Joined: Feb 03, 2001
Posts: 142
Originally posted by Brian Snyder:
I thought static meant it could only be accessed once? or first?

rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
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.


Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Brian Snyder
Ranch Hand

Joined: Feb 03, 2001
Posts: 142
Thanks to all. I'm ex"static" about it all now!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing methods from other classes.