Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reg static

 
Rajaraman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everybody,
Anybody explain me about STATIC with nice a example.I am always confused with that.
thanx.
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Rajaraman!
public class Q {
static int numOfObjects;
public Q() {
numOfObjects++;
}
public static void main(String[] args) {
new Q();
new Q();
new Q();
System.out.println(Q.numOfObjects);
}
}
This little class illustrates what static does. Static members are NOT associated with any special objects, but rather with the class as a whole (EVERY object created of the class).
The simplest usage of static is to create a variable that keeps track of how many objects that have been created of the class. This is what the varable 'numOfObjects' does in the above code. Every time you create an object using the new keyword, the counter 'numOfObjects' gets incremented.
You can reach a static variable (or method) by using the class name and the dot operator - as above: Q.numOfObjects. You never need an instance of a class to reach its static members.
I hope this helps!
/Kaspar
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi rajaraman,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.

http://www.javaranch.com/name.jsp


Thanks for your cooperation.
------------------
Co-Moderator of the Programmer Certification Forums
[This message has been edited by Thomas Paul (edited July 10, 2001).]
 
Raja Raman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi kaspar,
nice example.i got what static is.the situation i use to get confused with is - suppose we have a localvariable named str and a static variable named also str.
when i attempt to System.out.println(str);
in the main method what gets print.
thanx.
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, Raja! It is good that you now follow the name policy!
Take a look at this example:
public class Q {
static int i = 4; //static variable i
public static void main(String[] args) {
int i = 3; //local variable i
//This prints the local variable i, which shadows the static one
//in this method!
System.out.println(i);
//This prints the static variable i. We make sure this happens by
//using the classname and the dot operator!
System.out.println(Q.i);

//Another way of reaching the static variable i is by creating an
//object of the class. Then we can reach the static variable i by
//using the object reference. NOTE: the static variable i will be
//the same for ALL objects!
Q q = new Q();
Q q2 = new Q();
q.i = 2;
System.out.println(q.i);
System.out.println(q2.i);
//Same result!!!

}
}
Hope this helps!
/Kaspar
 
Raja Raman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai,
nice!!.my confusion over static is now clear.
now i can use static anywhere without any ambiguity.
thanks Kaspar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic