• 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

Confusion in static methods

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please indent your code; unformatted code is unnecessarily difficult to read.
 
reply
    Bookmark Topic Watch Topic
  • New Topic