Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Head First Java Confusion

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am new to Java.
What does this statement really mean ??

"You must keep in mind that these static(global like )thing eg- pi , are the exception rather than the rule in Java. They represent a very special case, where you have multiple/instances."

Thank You.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not have this book, but I suppose the answer to your problem could be found in the following tutorial on static variables,

tutorial

Have a look at the bicycle example and see how the numberOfBicycles static variable is used. I hope this helps.
 
Marshal
Posts: 74654
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A static member is one which is shared between all instances of the class, and exists even when there aren't any instances. There are several places where they can be used:
  • Constants: the example given, "pi" appears in the java.lang.Math class as "public static final double PI = 3.141592653589793". This is a value which is always the same, so there need only be one copy of them. You can use a static final field instead of a global method; it can be public because it can't be changed.
  • Counters and similar. You can use a static variable, which is shared between instances as a counter, as in the serial number example in the link which Abdulla Mamuwala gave earlier.
  • You can use a static method whenever there is no information which might change any of the fields of an object. Example: if you want the logarithm of a number, you send a number to the log() method and it gives you a result. No need to alter anything else. Look at the Math class: all its methods are static.

  • Example: you want a factory method which creates an instance of something: tell it what to create and it returns the instance. No change to anything else in an instance.Static members are less common than instance members, and that true constant values like PI are even less common, but if you look through the API you find quite a lot of static fields and methods.
     
    Shivit Agarwal
    Ranch Hand
    Posts: 82
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank You everybody. I got it now.
    [ March 03, 2008: Message edited by: Shivit Agarwal ]
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic