File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt with declaring local variables as satic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt with declaring local variables as satic" Watch "Doubt with declaring local variables as satic" New topic

Doubt with declaring local variables as satic

Supriya Nimakuri
Ranch Hand

Joined: May 23, 2006
Posts: 83
Which option most fully describes will happen when you attempt to compile and run the following code..

The output is given as option 4:..why cant be the output be option 2..since i has not initialized and used here..

public class MyAr{
public static void main(String argv[]) {
MyAr m = new MyAr();
public void amethod(){
static int i;

1) Compilation and output of the value 0
2) Compile time error because i has not been initialized
3) Compilation and output of null
4) Compile time error
Peter MacMillan
Ranch Hand

Joined: Jun 23, 2006
Posts: 34
You are correct in that i has not been initialized. However, the more important issue is the use of the static keyword.

Inside of an instance method, a static modifier makes no sense at all. Static variables must be fields of the class.
[ June 27, 2006: Message edited by: Peter MacMillan ]
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
First appropriate declaration; then comes initialization.

Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
There is only one modifier can be used by local variables that is final modifier.
I agree. Here's the link:
subject: Doubt with declaring local variables as satic
jQuery in Action, 3rd edition