static field and try block

M

marcwentink

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
 
T

Thomas Schodt

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);
}

}
 
P

Patricia Shanahan

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);
}
....

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
 
M

marcwentink

static initializer

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top