File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes reg static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "reg static" Watch "reg static" New topic
Author

reg static

Rajaraman
Greenhorn

Joined: Jul 09, 2001
Posts: 7
hi everybody,
Anybody explain me about STATIC with nice a example.I am always confused with that.
thanx.
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
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

Joined: May 05, 2000
Posts: 13974
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).]


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Raja Raman
Greenhorn

Joined: Jul 10, 2001
Posts: 4
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

Joined: Jun 18, 2001
Posts: 128
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

Joined: Jul 10, 2001
Posts: 4
hai,
nice!!.my confusion over static is now clear.
now i can use static anywhere without any ambiguity.
thanks Kaspar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reg static