File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about static" Watch "Question about static" New topic
Author

Question about static

Beginner Ben
Greenhorn

Joined: Jan 12, 2001
Posts: 5
Hello everybody,
I am happy to find this place recently and now hoping to get some help from you guys.
I tried to write some codes to help my understanding on File class in IO, but encounter some problems which may arise from somewhere else.
Please see the codes below:
import java.io.File;
public class FilePractice
{
File f1=new File("d:\\java2-certificate");

public static void main(String s[]){
boolean temp=f1.exists(); //boolean exists()
System.out.println("existance of f1 is "+temp);
}
}
error: can not make static reference to nonstatic variable f1 in
FilePractice class.
I got the error message as above. After I move "File f1=new File("d:\\java2-certificate");" into main method, everything is fine. I do not quite understand what it really means. Why can't I code like this? ( I am always weak in anythin static in java).
On the other hand, examples in RHE often use import like
import java.io.File. If import java.io.*; can actually import anything you need in io package, why bother to type File. Will it really slow down the process of the program?
Thanks in advance. I appreciate your help.
Ben
Ananda Kashyap
Greenhorn

Joined: Jan 03, 2001
Posts: 23
Just instantiate the class in main as:
FilePractice fp = new FilePractice();
And refer everything with fp later in the main()
Beginner Ben
Greenhorn

Joined: Jan 12, 2001
Posts: 5
Thanks for your help. I rewrite my main() as
public static void main(String s[]){
FilePractice fp = new FilePractice();
boolean temp=fp.f1.exists(); //boolean exists()
System.out.println("existance of f1 is "+temp);
}
I guess this is what you mean. However, I still do not understand why my first codes does not work. Why can't I call an File instance constructed in the same class inside my main mehtod?
Ben
James Baud
Ranch Hand

Joined: Jan 06, 2001
Posts: 60
Your error message is as it comes. main() is a static method and you cannot reference f1 as it is because the declaration from inside the class makes it an instance variable. You could either do it Ananda's way or declare your f1 static:
static File f1=new File("d:\\java2-certificate");

Read more on static and non-static members - very important basics.
Read Thinking in Java 2 (TIJ) - Bruce Eckel a very popular book in Java Ranch
[This message has been edited by James Baud (edited January 12, 2001).]


<B>~James Baud</B><P>He who asks, is a fool for five minutes;<BR>but, he who does not ask, remains a fool forever. (Chinese proverb)
summer_gsr
Greenhorn

Joined: Jan 12, 2001
Posts: 7
Ben,
I had the same doubt today and this is what I figured out, main is a static method and you cannot refer any non static methods or variables in a static method. In your example you are refering to a non static variable f1 in a static method main,so it wouldn't work. It is not the question of it being in the same class it is just that non static variables and methods cannot be refered in a static method.
Below is a small example I used to understand the concept:
class StaticTest
{
int age;
public static void main(String args[])
{
age=10;
System.out.println(age);
}
}
Here, age is a instance variable and Iam refering that in a static method main so I got a complilation error saying 'Can't make a static reference to nonstatic variable age in class Static'
But if you say int age=10; in the main and notice that they is no problem even if age is a instance variable.
Hope this helps,
Summer


GSR
Anand Rama
Greenhorn

Joined: Jan 12, 2001
Posts: 2
Ben,
To clearly understand the philosophy behind the solutions given, a small description of static modifiers might help. Let me try to do that.
When you attach a static modifier to a class variable, what you are essentially saying is, that variable belongs to the class and not to a particular instance as it would normally be; in the case of a normal variable. Normal variables are duplicated for every instance while a static variable is attached to the class and is shared by all the instances of the class.
Applying the same principle to static methods, static methods belong to the class and hence cannot access instance variables. 'f1' in your example is accessed in 'main' which is static while f1 is an instance variable.
Hence cannot be accessed. I hope this description was simple enough to clarify your question.
- Anand
Vladimir Kositsky
Ranch Hand

Joined: Nov 03, 2000
Posts: 116
File f1=new File("d:\\java2-certificate");
This statement makes three things:
1. Declaration of variable f1 type File, visibility - package("default");// check out access modifiers
2.Instantination of object File
3.Initialization of variable and assigning it refernce to herewith new-created object File
Default members(variables and methods) invisible from the body of static method( body includes parameters[ () ] as well).
Beginner Ben
Greenhorn

Joined: Jan 12, 2001
Posts: 5
Thanks a lot to all you guys. I do appreciate your help.
I still have one more question about the import statement, why not just import.io.* for all?
Thanks again and have a great weekend.
Ben
James Baud
Ranch Hand

Joined: Jan 06, 2001
Posts: 60
Ben,
For this program, "import java.io.*" will work as File is a class defined in this package.
However, if you were thinking "import java.io.*" and "for all" as conceptually the same as that of a file directory and its subdirectories (c:\win & c:\win\*.*) then that's a mistake. As an example, "import java.awt.*" does not recursively import subpackages, i.e. this will not give you access to types in subpackage "java.awt.event.*".
I think you'll get a better chance of getting a new question answered if you don't "encapsulate" it in the same thread. Post them as a new thread. Just a suggestion.

------------------
~James Baud
Talk, does not cook rice. - Chinese Proverb
[This message has been edited by James Baud (edited January 15, 2001).]
Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
Sure. Thanks. BTW, I had to change my name due to the ruls.
Ben
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about static