• 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

Static variables

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i am new to Java, reading Head First Java. Have some doubt about keyword "static". The book explained it well enough, but still have some doubts. So it'd be great to have help from you people..!!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gaurank Choudhary wrote:Well i am new to Java, reading Head First Java. Have some doubt about keyword "static". The book explained it well enough, but still have some doubts. So it'd be great to have help from you people..!!


The easiest way I know of explaining it for a variable is 'belongs to the class'. That is, there is exactly one variable for the class that it's defined in. Without the static modifier, there is one variable for each object.

And BTW, before you start creating lots of them, static variables should be used very sparingly (if at all). My suggestion would be not to define any until you know when and where they make sense.

Winston
 
Gaurank Choudhary
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well that helped a lot. But one more thing, why the main() method is made static..??? i guess this would clear all my doubts regarding static if you explain this concept.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gaurank Choudhary wrote:Well that helped a lot. But one more thing, why the main() method is made static..??? i guess this would clear all my doubts regarding static if you explain this concept.



That is how it is defined in the Java Language Specification... as part of the JVM startup.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.1


I guess, in theory, it could have been made *not* static, and the startup process could have included instantiating an object in order to call the method, but it wasn't.

Henry
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gaurank Choudhary wrote:Well that helped a lot. But one more thing, why the main() method is made static..???


And just to add to Henry's post - probably because that's the way C/C++ does it. Java is like a "well-behaved child" of those two languages, but the original designers probably started out with them, so couldn't throw out all the baby with the bathwater. Just my guess though.

i guess this would clear all my doubts regarding static if you explain this concept.


I doubt it, but what I would definitely recommend is NOT defining static variables because you think you have to. Those compiler errors are coming from the fact that you haven't created any objects yet. And fairly soon you'll learn not to put all your code in the main() method (in fact, almost none of it).

Winston
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void main(String[] args) Why is main static?

Put it simple.

To access non-static variable/methods, an object must be created.

At the start of a program, no objects exist. (that's not entirely true, but it can be seen this way)

If no objects exist, how can you call non-static variable/method?

You can see it this way.
 
reply
    Bookmark Topic Watch Topic
  • New Topic