GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Tech FAQs  >  Programming  >  Java
Go To First  |  Previous Question  |  Next Question 
 Java  |  Question 8 of 928    Print  
Use of static,final variable

  
Total Answers and Comments: 5 Last Update: May 14, 2009   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Sumit Kumar
 
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 


Above answer was rated as good by the following members:
rajesh.manem
July 18, 2005 09:43:27   #1  
kailash        

RE: Use of static,final variable
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

 
Is this answer useful? Yes | No
August 10, 2005 10:28:24   #2  
padmareddy        

RE: Use of static,final variable
can we acces static vairable with object

 
Is this answer useful? Yes | No
August 30, 2005 05:46:53   #3  
Sumit Kumar        

RE: Use of static,final variable
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

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
September 26, 2005 15:04:23   #4  
Paramesh        

RE: Use of static,final variable

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.


 
Is this answer useful? Yes | No
May 14, 2009 03:01:16   #5  
rajesh.manem Member Since: July 2007   Contribution: 2    

RE: Use of static,final variable
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.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape