• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Static method and null

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. public class TestRef {
2.
3. public TestRef () {
4. return ;
5. }
6.
7. public void print () {
8.System.out.println ("Instance") ;
9. }
10.
11. public static void sprint () {
12.System.out.println ("Class") ;
13. }
14.
15. public static void main(String arfs [ ]) {
16. TestRef t1 = null ;
17.TestRef t2 = new TestRef() ;
18.t1.sprint() ;
19.t1.print() ;
20.t2.sprint() ;
21.t2.print() ;
22. }
23. }

Why it gives error at 19 and not 18. It has got something to do with the method being static. Please clarify
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly. You know that it's not necessary an object to invoke a static method. In fact it can be invoked even on a null instance! However you'll need an object to invoke an instance method.
 
reply
    Bookmark Topic Watch Topic
  • New Topic