• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about static

 
Beginner Ben
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just instantiate the class in main as:
FilePractice fp = new FilePractice();
And refer everything with fp later in the main()
 
Beginner Ben
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
summer_gsr
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anand Rama
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. Thanks. BTW, I had to change my name due to the ruls.
Ben
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic