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();
//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
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
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();
//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
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);
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.