Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

static field and try block

Thread view: 
marcwentink@hotmail.com - 23 Mar 2006 12:52 GMT
Dear group,

I have written the following class below, which is a class to write
something in a log file. Now I want to make vars file, out and method
write(pStr) static, so I do not have to initialize an object when I use
this class.

Nevertheless when I make file static variable I encounter a problem. If
I try to initialize file in the code following "private BufferedWriter
out;" javac complains it wants to see a try, catch block. But when I
use the catch block, it gives me an error too. If I initialize the
static vars in the constructor I still have to make an object (once) to
use the static method since I have to be sure the static vars are
initialized, if I do not file for example is null. I am a but stucked
here now.

The latest thing I tried is copied, this gives a compile error below:

compile:
   [javac] Compiling 2 source files to E:\cvs\PROJE~9B\java\pd\build
   [javac] E:\cvs\PROJE~9B\java\pd\s\n\a\pd\LogIt.java:11: i
llegal start of type
   [javac]     try
   [javac]         ^
   [javac] E:\cvs\PROJE~9B\java\pd\s\n\a\pd\LogIt.java:49: <
identifier> expected
   [javac]     }
   [javac]                ^
   [javac] 2 errors

import java.io.*;

public class LogIt
{
    private static File file;
    private static BufferedWriter out;

    try
    {
        file = new File("logs/PGLogFile.txt");
        out  = new BufferedWriter(new FileWriter("logs/PGLogFile.txt",
true));
    }
    catch (IOException ex)
    {
        System.out.println("EventLog: failed opening event log with " + ex);
        System.exit(1);
    }

    public LogIt()
    {
/*         try
        {
            file = new File("logs/PGLogFile.txt");
            out  = new BufferedWriter(new FileWriter("logs/PdfGenLogFile.txt",
true));
        }
        catch (IOException ex)
        {
            System.out.println("EventLog: failed opening event log with " + ex);

            System.exit(1);
        }
*/     }

    public static void write(String pStr)
    {
        try
        {
       out.write(pStr);
       out.close();
        }
      catch (IOException ex)
      {
            System.out.println("EventLog: failed opening event log with " + ex);

            System.exit(1);
      }
    }   
}

Appreciation in Advance, Marcus Wentink
Thomas Schodt - 23 Mar 2006 14:32 GMT
> public class LogIt
> {
>     private static File file;
>     private static BufferedWriter out;

    static {

>         try
>        {
>          file = new File("logs/PGLogFile.txt");
>          out  = new BufferedWriter(
            new FileWriter("logs/PGLogFile.txt",true));
>        }
>            catch (IOException ex)
>        {
>         System.out.println("EventLog: failed opening event log with " + ex);
>         System.exit(1);
>        }

        }
Patricia Shanahan - 23 Mar 2006 14:33 GMT
...
> public class LogIt
> {
[quoted text clipped - 12 lines]
>         System.exit(1);
>     }

...

Your problem is executable code in a context in which only declarations
are permitted, outside any method body or block. If the intent is to
initialize static variables, how about a static initializer?

static{
 // Your static initialization code goes here
 // It will run when the class is initialized
}

Patricia
marcwentink@hotmail.com - 23 Mar 2006 14:40 GMT
> static initializer

Hey thanks a lot, I was not aware of the static initializer, so another
new java concept is learned today!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.