• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static variable confusion

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic