• Post Reply Bookmark Topic Watch Topic
  • New Topic

non-static method  RSS feed

 
sonali patil
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you try to call a non-static method ,but the non-sttic method doesn't use nay instance variables.Will the compiler allow taht?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. A static method in a class definition cannot directly call a non-static method in the class definition.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can not understand ! Can you give an example.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot call a non-static method from a static context without specifying the object you're calling it on - and it doesn't matter if the non-static method does or does not use any instance variables.

Why don't you write a small program to try it out yourself?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!