GeekInterview.com
Series: Subject: Topic:
Question: 1319 of 2109

Use of static,final variable

Asked by: Interview Candidate | Asked on: Aug 30th, 2005
Showing Answers 1 - 7 of 7 Answers
kailash

Answered On : 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

Yes  1 User has rated as useful.
  
Login to rate this answer.
padmareddy

Answered On : Aug 10th, 2005

Can we acces static vairable with object 

  
Login to rate this answer.
Sumit Kumar

Answered On : 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 

Yes  1 User has rated as useful.
  
Login to rate this answer.
Paramesh

Answered On : 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  varibleit 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.      

  
Login to rate this answer.
rajesh.manem

Answered On : May 14th, 2009

View all answers by rajesh.manem

  
Login to rate this answer.
muralidhar.9e

Answered On : Nov 22nd, 2010

View all answers by muralidhar.9e

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  keywordsby using this keywords we make the variables as static and final.We can use both of them as:   static final int a;

  
Login to rate this answer.

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!

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.