wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Variable Scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variable Scope" Watch "Variable Scope" New topic
Author

Variable Scope

Greedy thomas
Ranch Hand

Joined: May 15, 2003
Posts: 37
Hello There

I'm having a problem understanding the scoping of variables.

This is my understanding - correct me if I'm wrong.

Static means that the variable/method can be accessed from anywhere within the class. I assume that this means that it can be accessed within the methods of the class and also within any other classes.

Public means that any other class can access the variable/method. Does this class have to be in the same .java file.

What is the default if no access modifier is specified. What is the scope of the variable/method of those declarations.

I've enclosed the following program. When I declare testArray as static, the program compiles and runs correctly. However, if I omit the static or declare testArray to be private, I get the following compilation error message :

testArray.java:14: cannot resolve symbol
symbol : variable length
location: class testArray
for (int i = 0; i < testArray.length; i++) {
^
testArray.java:15: non-static variable testArray cannot be referenced from a static context
if (testArray[i] == stockID) {
^

Can I not have it as private or leave out the access modifier and then write methods to manipulate it?

Also, why do I have to have the method checkStock to be static and/or public. Can I not leave these out. When I do leave these out, I get the following compilation error :

testArray.java:25: non-static method checkStock(java.lang.String) cannot be referenced from a static context
if (checkStock("Four")) {
^

I had wanted this array (testArray) to be private since it must only be known by this class. This is why I had tried to declare it as private initially but kept getting errors. I thought if I could declare the array as private, then I could declare the checkStock method as private as well, since the method will only be used in this class. How do I go about declaring a private member of a class and what signature must the methods that access these private members have.

I suppose I'm missing something very basic and important here but not able to fully figure it out. Do I always have to declare the variables as static to allow me to access then within methods.

Thanks in advance for your assistance.

Regards,
Rajesh


Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
A static method is associated with a class, but is not associated with any instance of the class. The main method is static, so it can be invoked even when no instance of the class exists. Your checkStock method is also static, so checkStock can be invoked from the main method even when no instance of class testArray exists. If you like, you can also add the "private" modifier.

private static boolean checkStock(String stockID)

If you remove the static modifier, then checkStock would become a non-static instance method. If checkStock is an instance method, then it can only be invoked on an instance of class testArray. Your main method could create the instance of testArray and then invoke the checkStock method on it.



Your testArray class shares its name with a static field named testArray. Java class names typically begin with uppercase letters, so you might want to change the name of your class.

Since your testArray field is static, it is appropriate that your checkStock method is also static. If you want to have a non-static checkStock method, then you are saying that checkStock is a method that checks something that is associated with an instance of the testArray. If that is the case, then it would be appropriate for the testArray field to also be non-static.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
I beg to differ, I don't see anything wrong with using a non-static method that uses a static variable in its codes. Its perfectly legal too. Appropriateness, is subjective.

Just my 2c


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Cheng Wei Lee:
I beg to differ, I don't see anything wrong with using a non-static method that uses a static variable in its codes. Its perfectly legal too. Appropriateness, is subjective.

Just my 2c


I agree that it is perfectly legal to use an instance method to access class data (a.k.a static data) that has nothing to do with a particular instance of the class. The disadvantage of using an instance method to access class data is that the class data will not be accessible until an instance of the class is created. Using a static method to access class data removes that restriction.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variable Scope
 
Similar Threads
accessing private members
NOTES FROM VELMURUGAN'S SITES:
about MMMMMModifiers-fires
Inner Class inside a method
Need some help with Java program Airline.java