• 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

class methods

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class method can access only the class variables and class methods of its class..

How much is the above sentence correct ? I am confused since I can run the following program which looks to me contrary to the above .

public class Test{
public static void main(String arg[]){
Test t = new Test();
t.amethod();
}
public void amethod(){
}
}
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Statement is true. One can't access non static code from a static method directly by their simple names.
In the program, Instance of Test is created and hence accessing its methods(both static and non static.) would not create any problem.

But incase if you try to call the non-static method without creating an instance of Test, then a compile time error is generated.

public class Test{
public static void main(String arg[]){
amethod(); // gives compile time error.
}
public void amethod(){
}
}

[ June 02, 2006: Message edited by: pramila ch ]
[ June 02, 2006: Message edited by: pramila ch ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic