• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

variables in static methods

 
muskaan gaffor
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..!!
 
muskaan gaffor
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-------------------------------------------------------------

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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-------------------------------------------------------------

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?

--------------------------------------------------------------------------------
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First tell me what is the difference between local variable and instance variable?
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic