Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Really Really Confused

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a question which I am not able to understand at all� I am trying to run the below program.
class Test
{
public static void main(String args[])
{
int x=1;
//System.out.println(x);
System.out.println(Test.x=100);
}
}

As U can see I am trying to modify the variable x value from 1 to 100. The error I am getting is �Cannot find symbol: variable x�

Then I am taking off the commented lines and running the program. Now it�s showing that the variable x value is 1 but then why am I not able to change its value?/


Any help would be greatly appreciated as I am just starting out.

Thanks.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in your code, the variable 'x' is a METHOD variable, not a CLASS variable. since it is declared inside the main() method, that is where it lives, and why you can print it there.

if you changed your code to this:



i think it will work (it's late, and i haven't tested it)
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The line "Test.x=100" is trying to set a static variable of the Test class, named x, to the value of 100. Unfortunately, there is no such variable. This...



Doesn't declare a static int variable. It declares a local variable, within the scope of the main method.


[EDIT: Beaten to the response again... ]

Henry
[ December 23, 2007: Message edited by: Henry Wong ]
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fred and Henry,

Thanks for the reply. I can understand that "x" in my program is a method variable. But I am also trying to change it's value in the same method right? Then where does the class variable issue come from?

Thanks.
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But I am also trying to change it's value in the same method right? Then where does the class variable issue come from?



You are trying to change a class variable, in the same method. The computer only does what you ask it to do -- it doesn't know that since there isn't a class variable, and there is a local variable that is in scope with the same name, that is what you meant.

You asked to change a class variable, it looked for a class variable.

Henry
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arjun.

I think I am getting your doubt. When you write you are printing the value of local variable 'x'.

But when you write you are trying to access the static variable 'x', which Henry and Fred have referred to as 'Class' variable.

There is slight error in Fred's code. To make your program work, do this:


Now you have defined x as a static variable and it is available. Simple na?
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To know more about the static variables you can see this one

Static Variable
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok.. This is what I have understood so far guys..

In a static method, one can only access (manipulate) a static variable only. Since there is no static variable x, I am getting the error.

I however have one more question. according to what you have all said, A variable declared in a static method does not become static by default right?

If thats the case, I tried doing this

class test
{
public static void main(String args[])
{
static int x=1;
//System.out.println(x);
System.out.println(test.x=100);
}
}

I have declared x as static and now I am getting errors too. I am just playing around with the code. The way U guys asked me to do worked but then still I was wondering why the above code would not work since now, x is declared as static?/

Thanks
[ December 24, 2007: Message edited by: Arjun Reddy ]
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unlike C, you cannot have static variables inside methods. The only way to have static variables, is declare them inside the class, not the method, as you've already been shown how to do it.

A variable declared in a static method does not become static by default right?


No it doesn't. Instead, once the method ends, the variable and its value will be discarded, never to be seen again. If you execute the method again, you will just create another variable with the same name and same initial value.

If the variable pointed to an object, and you have another reference to that object outside the method, the object itself is not discarded of course. Only the variable's value, which is a reference to that object, is discarded.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes a lot of sense Rob. Thanks guys to all of you for helping me out understand it.


Thanks.

[ December 24, 2007: Message edited by: Arjun Reddy ]
[ December 24, 2007: Message edited by: Arjun Reddy ]
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't you use the name class for one variable local.
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic