• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion in static methods

 
sanchit latawa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,I'm confused regarding the use of static method.As you can only call static methods and variables inside a static method .So this means

This is illegal

But than why is this legal ???


Also you can not use any non static variable inside a static method but when i do something like

So this works but if i was declared outside main and I tried to use it this would not.Also if i declare a variable or method inside static method .Does it automatically become static ??
THanks
 
Prashant Hurria
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is illegal becuse you are reffering a non static method from a static code. Non static members have to be assosiated with a object.





So this works but if i was declared outside main and I tried to use it this would not.Also if i declare a variable or method inside static method .Does it automatically become static ??


Dont be confused between static and local variables. Read a basic book on Java like Head First
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please indent your code; unformatted code is unnecessarily difficult to read.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic