Results 1 to 5 of 5
- 08-01-2007, 12:02 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 8
- Rep Power
- 0
- 08-01-2007, 01:31 PM #2
Member
- Join Date
- Aug 2007
- Posts
- 15
- Rep Power
- 0
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
}
}
- 08-02-2007, 05:09 AM #3
Senior Member
- Join Date
- Jul 2007
- Posts
- 130
- Rep Power
- 0
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
- 08-02-2007, 05:50 AM #4
Member
- Join Date
- Jul 2007
- Posts
- 8
- Rep Power
- 0
Thanks for ur reply.static variables are clarified but pls clarify more on static methods.
- 08-02-2007, 09:52 AM #5
Member
- Join Date
- Aug 2007
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
accessing instance variables from static methods
By ravian in forum New To JavaReplies: 7Last Post: 03-01-2009, 10:09 PM -
Using Static Variables
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:08 PM -
Local Variables for a static method - thread safe?
By mikeg1z in forum Advanced JavaReplies: 1Last Post: 11-16-2007, 01:06 AM -
Static methods
By Java Tip in forum Java TipReplies: 0Last Post: 11-04-2007, 05:56 PM -
Help with static variables
By bbq in forum Advanced JavaReplies: 1Last Post: 06-28-2007, 05:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks