• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing methods from other classes.

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brian Snyder:
I thought static meant it could only be accessed once? or first?


 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all. I'm ex"static" about it all now!!!
 
Danger, 10,000 volts, very electic .... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic