Use of static,final variable

Showing Answers 1 - 7 of 7 Answers

kailash

  • Jul 18th, 2005
 

1 if class is final we can't inherit(extends) it 
2 if a method is final we can't override it 
3 if a variable is final it work as constant 
::any abstract method can't be finaland vice-versa:: 
 
static represent whole class not a particular object

padmareddy

  • Aug 10th, 2005
 

can we acces static vairable with object 

  Was this answer useful?  Yes

Sumit Kumar

  • Aug 30th, 2005
 

Yes, You can access static variable with object reference also. 
 
Static Variable: 
Declaring a member variable to be static causes it to be a class variable. Local variables cannot be declared static. Only member variables can be declared static. To begin with, don't ever use static variables without declaring them final unless you understand exactly why you are declaring them static. Static final variables, or constants, are often very appropriate. See the fields in the Color class for example. I can only think of a few situations where the use of a non-final static variable might be appropriate. One appropriate use might be to count the number of objects instantiated from a specific class. I suspect there are a few other appropriate uses as well. 
 
Static methods  
Don't declare methods static if there is any requirement for the method to remember anything from one invocation to the next. A static method only has access to other static members of the class, so it cannot depend on instance variables defined in the class. Declaring a method to be static causes it to be a class method. 
 
Example: 
The main method is a class method  
 
public static void main(String[] args) 

//statements; 

 
The important thing to note here is that the main method is declared static. That causes it to be a class method. As a result, the main method can be invoked without a requirement for an object of the class to exist. Also, the main method has direct access only to other static members. 
 
 
/*File MyClass01.java 
Copyright 2005, Sumit Kumar 
 
This program illustrates static members of a class. 
 
Output is: 
 
Static variable 
Mon Aug 30 09:52:27 CDT 2005 
 
Instance variable 
Mon Aug 30 09:52:32 CDT 2005 
Static variable 
Mon Aug 30 09:52:27 CDT 2005 
 
 
Instance variable 
Mon Aug 30 09:52:37 CDT 2005 
Static variable 
Mon Aug 30 09:52:27 CDT 2005 
**************************************/ 
 
import java.util.Date; 
class MyClass01 

static Date v1 = new Date(); 
Date v2 = new Date(); 
 
public static void main(String[] args) 

//Display static variable 
System.out.println("Static variable"); 
System.out.println(MyClass01.v1); 
 
//Delay for five seconds 
try 

Thread.currentThread().sleep(5000); 

catch(InterruptedException e){} 
 
//Instantiate an object and display instance variable 
MyClass01 ref1 = new MyClass01(); 
System.out.println();//blank line 
System.out.println("Instance variable"); 
System.out.println(ref1.v2); 
 
//Now, display the static variable using object reference 
System.out.println("Static variable"); 
System.out.println(ref1.v1); 
System.out.println();//blank line 
 
//Delay for five seconds 
try 

Thread.currentThread().sleep(5000); 

catch(InterruptedException e){} 
 
//Instantiate another object 
MyClass01 ref2 = new MyClass01(); 
System.out.println();//blank line 
System.out.println("Instance variable"); 
System.out.println(ref2.v2); 
 
//Now, display the same static variable using object reference 
System.out.println("Static variable"); 
System.out.println(ref2.v1); 
}//end main 
}//end class MyClass01 

Paramesh

  • Sep 26th, 2005
 

padmareddy Wrote: can we acces static vairable with object 

                we can access static variable by using objects also for example see the below program u will get clear idea on finalize(), on garbage collecter and  static  varible

it will explain that

  -> static variable is common to all objects

  -> finalize() will be called before grabage collection

 ->gc collect the memory of all unreferenced objects

 ->static block will be executed before main()

  public class Static

 {

  static int count;

  static()

   {

    count=0;

   System.out.println('from static block ");

   }

  Static()

    {

    count++;

   }

  public static void main(String _[])

  {

      Static s1=new Static();

      Static s2=new Static();

      Static s3=new  Static();

      System.out.println("total objected created"+count);

     s3=null;

     s2=null;

     s1=null;

  System.gc();

   }

public fianlize()

 {

  Sytem.out.println("remainig object(s):"+count);

  }

}

     regards

   Paamesh.A

   STEP Online Pvt Ltd. 

     

  Was this answer useful?  Yes

The static variable is used to Synchronize (Ex: in seaphores…) the threads because it maintains the single copy for all the instances. We can call the static variable by the class name or from the reference. It stored in a Data segment.

  Was this answer useful?  Yes

static variable allocates the memory as globally.It won't reallocate the memory while running.It initializes the memory for the variable only once.

final variable tells the variable is final means it is constant value in the program.

actually final and static are  keywords

by using this keywords we make the variables as static and final.

We can use both of them as:   static final int a;

  Was this answer useful?  Yes

Static : Move the variable/methods/class during HDD to RAM. ie; they are available without instantiating the class, by creating an object of that class nd access its methods,members.
Final : for Preventing the inheritance further ie; the class with final word as prefix cannot be extend further!

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions