wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes static variable confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static variable confusion" Watch "static variable confusion" New topic
Author

static variable confusion

Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
Hi,

public class Test
{
static int a;
int y=10;
public static void main(String[] args)
{
System.out.println(" y = " + y);
}
}

I tried to complie the above code, it's shows up error that non-static variable can't be referenced in the static method. Okay.

Then I tried the following code:

public class Test
{
static int a;
public static void main(String[] args)
{
int y=10;
System.out.println(" y = " + y);
}
}

Now I complied the above code, I got the result as y = 10. the int y has no static keyword, then how it is accepted by the static method
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
But in the second code you are declaring y in a the method where you are calling. So you dont need declare it is a static. You require to declare it as static only when you are declaring outside of static method and calling from static method
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Try adding the static keyword to the declaration for the variable y in the second version of your code. What happens?

Layne

p.s. For future reference, please use UBB CODE tags when posting your code. These will preserve your formatting so that it is easier for us to read.


Java API Documentation
The Java Tutorial
Archies Gomes
Ranch Hand

Joined: Jun 19, 2005
Posts: 34
Hi Niyas
See static member is one which is applicable to all the objects of the class and not only to a single calling object.Thus a static member changes all the objects which are created for that class.
In your this code,

public class Test
{
static int a;
int y=10;
public static void main(String[] args)
{
System.out.println(" y = " + y);
}
}
Here first of all,

You are not referencing y from any object......How does the compiler know what is "y".It dosent has any object associated with it.Here y is a class memeber and you need an object to refer to a class memebr .

And In your second code....
public class Test
{
static int a;
public static void main(String[] args)
{
int y=10;
System.out.println(" y = " + y);
}
}

Here y is a local variable thus it obviously can refer to it though it is not static ....In your previous code "y" was a class member here it is treated just like a local variable declared in a function.


Just go through this foll code.....
public class Test
{
int var1;
static int var2;
static void foo()
{
var1=10;//Error !!Static method refering a non static member.
var2=200;//Perfectly valid as this static member var2 is for the whole class and not only for the calling object
}
public static void main(String a[])
{
new Test().foo();
}
}

See here whenever an object is created each object gets a space or instance of var1(ie different var1's for different objects)....but var2 is such a variable which is for all the object of this class....and not to any individual objects(var2 is a varialbe shared by all the objects of this class).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static variable confusion