Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variable confusion

 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Archies Gomes
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic