Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-01-2007, 02:02 PM
Member
 
Join Date: Jul 2007
Posts: 8
imran_khan is on a distinguished road
significance of static variables and methods
Hi,
can anyone tell me the significance of using static variables and methodsin an elaborative way?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-01-2007, 03:31 PM
Member
 
Join Date: Aug 2007
Posts: 15
yiweiang is on a distinguished road
Hi,

In simple terms, Static methods and Variables can be called without being instantiated. In fact, they cannot be instantiated.

So you have a class Shirt.

public class Shirt
{
public static int quantity=1000;


//rest of the code

}

then you have two subclasses of Class Shirt

public class Jacket extends Shirt
{

psvm(String Args[])
{
System.out.println(Shirt.quantity); //prints out 1000
Shirt.quantity=Shirt.quantity-100; //Now quantity is 900
}

}

public class TShirt extends Shirt
{

psvm(String Args[])
{
System.out.println(Shirt.quantity); //prints 900 because they are referring to the same thing

}

}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-02-2007, 07:09 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
The keyword static also means that the variable declared as static are used as a class variable, in terms of that there exist only one copy of this variable in the memory, no matter how much instance that class have

If u don't use the static keyword, when each instance are constructed they would get each a acopy of the variable, but if it's declared static, then the variable refer to the class variable in which if u modify the variable in an instance, then the rest of the other instances also get the effect

Since it's static, and just like we have discussed before are considered as a class variable or class method, we don't need to instantiate an object to use it, instead we just use the classname.variable or classname.method() to call it
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-02-2007, 07:50 AM
Member
 
Join Date: Jul 2007
Posts: 8
imran_khan is on a distinguished road
Thanks for ur reply.static variables are clarified but pls clarify more on static methods.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-02-2007, 11:52 AM
Member
 
Join Date: Aug 2007
Posts: 15
yiweiang is on a distinguished road
Static methods are basically methods that need not be initiated to be called upon. Assuming that the class is in the same package, or if you have imported the class, just doing this will allow you to use the method:

theClassOfTheStaticMethod.methodName();

Yi Wei
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Static Variables Java Tip java.lang 0 04-17-2008 01:08 AM
Local Variables for a static method - thread safe? mikeg1z Advanced Java 1 11-16-2007 03:06 AM
accessing instance variables from static methods ravian New To Java 5 11-08-2007 10:53 AM
Static methods Java Tip Java Tips 0 11-04-2007 07:56 PM
Help with static variables bbq Advanced Java 1 06-28-2007 07:38 PM


All times are GMT +3. The time now is 10:07 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org