File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Really Really Confused Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Really Really Confused" Watch "Really Really Confused" New topic
Author

Really Really Confused

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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.


Be Humble... Be Nice.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

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)


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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
Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
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?
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

To know more about the static variables you can see this one

Static Variable
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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 ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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 ]
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
Can't you use the name class for one variable local.


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
 
Consider Paul's rocket mass heater.
 
subject: Really Really Confused
 
Similar Threads
Pass by value
Doubt about Final variable
initialisation and reinitialisation doubt.....
Not looping...
variable access issues - still disturbing....