Results 1 to 5 of 5
  1. #1
    imran_khan is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Red face significance of static variables and methods

    Hi,
    can anyone tell me the significance of using static variables and methodsin an elaborative way?

  2. #2
    yiweiang is offline Member
    Join Date
    Aug 2007
    Posts
    15
    Rep Power
    0

    Default

    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

    }

    }

  3. #3
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    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

  4. #4
    imran_khan is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Default

    Thanks for ur reply.static variables are clarified but pls clarify more on static methods.

  5. #5
    yiweiang is offline Member
    Join Date
    Aug 2007
    Posts
    15
    Rep Power
    0

    Default

    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

Similar Threads

  1. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 11:09 PM
  2. Using Static Variables
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:08 AM
  3. Local Variables for a static method - thread safe?
    By mikeg1z in forum Advanced Java
    Replies: 1
    Last Post: 11-16-2007, 02:06 AM
  4. Static methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 06:56 PM
  5. Help with static variables
    By bbq in forum Advanced Java
    Replies: 1
    Last Post: 06-28-2007, 06:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •