• 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

static method reference

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings once again.

After unsuccessfully testing the following code, which in a sample example on paper, the answer is:

package Testing1;

public class E {
void printS1() {
System.out.println("F.printS1");
}
static void printS2() {
System.out.println("F.printS2");
}
}
class F extends E {
void printS1() {
System.out.println("F.printS1");
}
static void printS2() {
System.out.println("F.printS2");
}
public static void main(String[] args) {
E x = new F();
x.printS1();
x.printS2(); // line with compile-time error message
}
}

Error message:

The static method printS1 from the type E should be acessed in a static way.

In Sierra Bates on page 103 it states at the top of the page that you can "use an object reference variable to access a static member".

Can someone help me with this?

Thank you,

Jerry B.
 
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If above mentioned code is in one file, it will compile but not run as main method is not in public class. Move the main method to public class and try to run it.

I got the following output without changing your code except that I moved the main method from class F to public class E.


Here's the code for above output
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I compiled and ran your code successfully. I'm using a JDK 1.4.2 and did not get any error messages.

Output:


Notice that the classes are in a package. Consider it when compiling/running your code.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compile error is in fact a warning (you can see it when using Eclipse). You can certainly access a static member though an instance of the class, but that is not the recommended way of doing it. Because static members are associated with the class rather than individual instances, the recommended way to access them is as <ClassName>.<MemberName>. So, in the case of your example, E.printS2() would not give the warning.
 
Jerry Bustamente
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Satish, Barry, and Pedro. Greatly appreciate your help.

:-)

JerryB
 
reply
    Bookmark Topic Watch Topic
  • New Topic