wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes variables in static methods 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 "variables in static methods" Watch "variables in static methods" New topic
Author

variables in static methods

muskaan gaffor
Ranch Hand

Joined: May 19, 2006
Posts: 50
int x=0; //line#1
public static void main(String d[]) { //line#2
while(x<=0) { // line#3

when i use the above code in my program, it will throw a error, cannot make a static reference to nonstatic fiel x.
---------------------

public static void main(String d[]) { //line#4
int x=5; //line#5
while(x<=0) { // line#6

this compiles fine.

the variables (non static) used in static method.why is not giving error?

regards,
Mussy
Nousheed Mohammed
Greenhorn

Joined: Mar 30, 2007
Posts: 6
if you declare x as an instance variable you cant use that x in a static mehod directly... to access that you have to create an instance of the class and on that instance only you can access the variable...
Originally posted by muskaan gaffor:
int x=0; //line#1
public static void main(String d[]) { //line#2
while(x<=0) { // line#3

when i use the above code in my program, it will throw a error, cannot make a static reference to nonstatic fiel x.
---------------------

public static void main(String d[]) { //line#4
int x=5; //line#5
while(x<=0) { // line#6

this compiles fine.

the variables (non static) used in static method.why is not giving error?

regards,
Mussy
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

First of all burn the following into your mind..

1) Static belongs to class

2) non-static belongs to Object


non static Variable and methods of a class are the properties which an Object inherits from class and has rights to modify them as per their own use..!! all non static variable and methods can talk with each other directly as they belongs to Object..!! Similarly all static Variable can talk with each other as they belongs to class..!!
When you try to access non static from a static property..you are actually trying to use an Object property from a class property..!!
How can you use someone else properties without permission..! So in order to access an Object property you need to take permission..!! in order to have permission you need to have Object..!!
so first create an Object..then try to access the property..!! when you try to access..Object will see if the requested property can be given to you or not..!! access modifier of properties will help an object to decide that..!!


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
muskaan gaffor
Ranch Hand

Joined: May 19, 2006
Posts: 50
-------------------------------------------------------------

int x=0; //line#1
public static void main(String d[]) { //line#2

classA a=new classA();

while(a.x<=0) { // line#3


will work fine
---------------------

public static void main(String d[]) { //line#4
int x=5; //line#5
while(x<=0) { // line#6

this compiles fine.

in this line#5 is not a static one.why is this not giving error?

--------------------------------------------------------------------------------
muskaan gaffor
Ranch Hand

Joined: May 19, 2006
Posts: 50
-------------------------------------------------------------

int x=0; //line#1
public static void main(String d[]) { //line#2

classA a=new classA();

while(a.x<=0) { // line#3


will work fine
---------------------

public static void main(String d[]) { //line#4
int x=5; //line#5
while(x<=0) { // line#6

this compiles fine.

in this line#5 is not a static one.why is this not giving error?

--------------------------------------------------------------------------------
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

First tell me what is the difference between local variable and instance variable?
Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
Originally posted by Sunny Jain:
First tell me what is the difference between

local variable:

It is a variable that is declared inside a method. Class instance not required to access and

instance variable :

It is a variable that is declared outside a method but inside a class. Available after making an instance of the class.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variables in static methods