Results 1 to 5 of 5
  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default A problem with PrintWriter, a question about static classes and FileNotFoundException

    Java Code:
    import java.util.*;
    import java.io.PrintWriter;
    import java.io.File;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    
    public class Main extends JFrame  {
    	
    	static File main= new File("Main.txt");
    	static PrintWriter output = new PrintWriter(main);
    	static Scanner read= new Scanner(main);
    	static ArrayList lessons= new ArrayList();
    	static ArrayList clients= new ArrayList();
    	static int earned,
    				paid,
    				debt;
    	boolean myDebt;
    	
    	void newClient(int fee, String fName){
    		
    		clients.add(new Client(clients.size(),fee,fName));
    		
    		
    	}
    	void newLesson(double duration, Client client){
    		
    		lessons.add(new Lesson(duration,client));
    		
    	}
    	
    	
    	public static void main(String[] args) throws Exception {
    		
    
    	}
    }
    Hi,

    first I'm not sure I'm doing the right thing with the declaration of this class, so I wanted to ask you guys.
    The reason I've declared it "static" is because this class supposed to be the main interface of the program and I don't need it to create more than 1 object. Is it wrong to use static for that reason? since I think it's the purpose of static classes, but not realy sure.

    About the PrintWriter, you could see above that I declared a new file and tried to use it for the Scanner and the PrintWriter. I understand that it's wrong since the file was not created yet (it's out of the constructor) and I can't really use it in the printWriter if it doesn't exist.
    I don't want to declare the printWriter in the constructor because I want it to be a common object to be used by all the methods of the class.
    I've also tried not to use the file and write the path to the file again specificly for the printWriter, like this:

    Java Code:
    static PrintWriter output = new PrintWriter("Main.txt");
    But then there's the problem of "FileNotFoundException" that the compiler (eclipse) shows me, I've been trying to write the Main class this way:

    Java Code:
    public class Main extends JFrame throws Exception {
    But then there're even more errors:

    Multiple markers at this line
    - Syntax error on tokens, AnnotationName expected instead
    - Breakpoint:Main
    - The serializable class Main does not declare a static final serialVersionUID field of
    type long
    - Syntax error, insert "}" to complete Block


    Sorry it's so long, but I didn't want to spread this onto 3 threads that concerns the same program.

    Thanks a lot for your patience!

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: A problem with PrintWriter, a question about static classes and FileNotFoundExcep

    Hi!

    This is interesting, because it's about the same level of questions that I often run into. Sometimes I think I understand something, but there is always something more to it.

    From what I have learned, you cannot declare a top-level class as static, this can only be done on nested classes. The simple reason for declaring the nested class static is to be able to use that class without having to instantiate the outer class first. A top level class should NEVER be declared static, from what I've learned. I'm not even sure it's possible to do it without the compiler complaining.

    Please correct me if I'm wrong! =)

    About the PrintWriter error, I would've probably ended up with the same error as you, so I wont dive into that.

    ---

    EDIT:
    I looked at the Java pages at Oracle and there's some good reading on nested/inner classes there with alot of information about static & non-static. From what I get, what I said above was partially correct but as always there is alot more to it!

    Nested Classes
    Last edited by Zyril; 08-10-2012 at 03:12 PM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: A problem with PrintWriter, a question about static classes and FileNotFoundExcep

    I would simply get rid of all the static stuff and make a normal everyday class with attributes.
    In the constructor you can then initialise those attributes and at least that way you can put some proper exception handling in.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: A problem with PrintWriter, a question about static classes and FileNotFoundExcep

    Thanks guys,
    I've added them into the constructor and removed the "static", though it still bugs me I don't know how to use the static right.. that's why I'm always trying to use it as much as possible until I get the idea.
    This isn't a class assignment but a program I'm going to actually use, so I'm trying to make it as best as I can. I think that without the static class I would've wasted a lot of memory, because I need only 1 object of JFrame and I don't need the option to create more.

    I'll read about the nested classes in the link!

    Thanks again for having the patience to read all this :)

  5. #5
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: A problem with PrintWriter, a question about static classes and FileNotFoundExcep

    Quote Originally Posted by Mapisto View Post
    Thanks guys,
    I've added them into the constructor and removed the "static", though it still bugs me I don't know how to use the static right.. that's why I'm always trying to use it as much as possible until I get the idea.

    I'll read about the nested classes in the link!

    Thanks again for having the patience to read all this :)
    I do not think it's a good idea to use something you feel uncertain of as much as you can. Doing this will get you into the habit of just doing it, and you will fool your brain into thinking that this is the way it is supposed to be done. Even though you know it, subconciously it will still be there. My first Java teacher told me not to use static at all, until I found that I had to use it. I think that has worked pretty well so far!

Similar Threads

  1. Replies: 3
    Last Post: 12-27-2011, 04:17 AM
  2. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  3. printWriter problem
    By Lund01 in forum Advanced Java
    Replies: 6
    Last Post: 10-14-2010, 01:28 PM
  4. FileNotFoundException problem
    By doha786 in forum New To Java
    Replies: 1
    Last Post: 04-19-2010, 11:07 AM
  5. static vs. non-static nested classes
    By rinke in forum Advanced Java
    Replies: 8
    Last Post: 06-30-2009, 07:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •