Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why Static? Redeaux 3

Status
Not open for further replies.

KirbyWallace

Programmer
Dec 22, 2008
65
US
In my attempts to escape the static dungeon in which I seem to have found myself, I've modified my previous attempt at this to the following:

Code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileToConsole {

    String cLine = null;

    public FileToConsole(String cFile) {

        try {

            BufferedReader input =  new BufferedReader(new FileReader(new File(cFile)));

            while (( cLine = input.readLine()) != null) {
                 System.out.println(cLine);

		// Add the same line to the jTextArea on the
		// MainFrame Panel, IF it is open...
		// how to check that it is open???

		// ERROR ON FOLLOWING LINE: non-static variable 
		// txt_OutputWindow cannot be referenced from 
		// a static context
		MainFrame.txt_OutputWindow.setText(cLine);
            }
        }

        catch (IOException ex) {
            System.out.println("File Not Found");
        }
    }
}

Why does it think that it's in a static context? Nothing about the class (or the constructor) indicates staticicity...

Are constructors themselves static by default?


What I am trying to accomplish is pretty simple. "MainFrame" is a swing form which contains the text field (txt_OutputWindow) you see me trying to write to.

The only tricky bit is maybe that it's a button handler on that form that has created this instance of FileToConsole that I've presented here. The form calls the class, and I want the class to be able to write back onto one of the form's text fields.

If anyone can see through what I'm doing, to what I'm trying to do, please feel free to chime in.

 
Code:
import java.awt.*;
import javax.swing.*;
public class MyJFrame extends JFrame{
       static JTextArea jTextAreaObj = null;
       public MyJFrame() {
              getContentPane().setLayout(new BorderLayout());
              jTextAreaObj = new JTextArea("JTextArea");
              getContentPane().add(BorderLayout.CENTER,jTextAreaObj);
              JButton jButtonObj = new JButton("Click");
              getContentPane().add(BorderLayout.SOUTH,jButtonObj);
       }
       public static void main(String args[]){
              MyJFrame myJFrameObj = new MyJFrame();
              myJFrameObj.setSize(400,400);
              myJFrameObj.setVisible(true);
              myJFrameObj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
              MyFileToConsole myFileToConsoleObj = new MyFileToConsole();
              myFileToConsoleObj.fileToConsole("hello.txt",jTextAreaObj);
       }
}

import java.io.*;
import javax.swing.*;
public class MyFileToConsole{
    public void fileToConsole(String cFile, JTextArea jTextAreaObj2) {
        try {
            BufferedReader input =  new BufferedReader(
                                    new FileReader(
                                    new File(cFile)));
            String line = null;
            while (( line = input.readLine()) != null) {
                 System.out.println(line);
                 jTextAreaObj2.append(line);
            }
        }
        catch (IOException ex) {
            System.out.println("File Not Found");
        }
    }
}
You need to have a file called "hello.txt".
 
Java is complaining that the text field txt_OutputWindow is not a static field in the Mainframe class. Either make it a static field or create a getter method in the Mainframe class to obtain a handle to the text field.
 
cljen: Thanks! The "make it static" thing was an immediate fix. I just need to follow Dian's advice and read up on this whole scope and visibility thing.

prosper: Thanks, that code worked well. Just what I was trying to do myself!

ALL: I'd still like to know why the compiler thinks "FileToConsole()" is static. Nothing in it's declaration indicates "staticicity". Is there something about the WAY I called or created it that made it static?


Thanks all!
 
Some of my terminology might be off. FileToConsole is a class. You can call static methods within a class without creating an instance of it. If you want to call non static methods, you need to do it using an instance of the class
Code:
FileToConsole.staticMethod();//is ok
FileToConsole.nonStaticMethod();//not ok

FileToConsole ftc = new FileToConsole();//ftc is an instance of the FileToConsole class
ftc.staticMethod();//I think it works but it's bad form
ftc.nonStaticMethod();//is ok

-----------------------------------------
I cannot be bought. Find leasing information at
 
String cLine = null;
is never used outside the method/Constructor, so there is no reason why to make it an attribut.

Au contraire! It is only used in the while-loop and should be declared therein.

You should allways choose the smallest scope possible.

don't visit my homepage:
 

Thanks all... Everyone's advice helped much.

However, I still have the question, Why does the JVM think that this code is in a static context?

Why do I get an error on that "MainFrame.txt_OutputWindow.setText()" line that says "Non-Static variable cannot be accessed from a static context"?

I just do not see why this code is static.


Again, thanks all...
 
I think that referencing the class name (MainFrame) is always static context. You'd have to create an instance of it to use non static variables and methods.
Code:
MainFrame mf = new MainFrame();
mf.txt_OutputWindow.setText();

-----------------------------------------
I cannot be bought. Find leasing information at
 
Jax...

That last comment of yours and the one you made earlier were both very helpful. In fact, I wrote it down: "You can call static methods within a class without creating an instance of it."

That alone speaks volumes. The fact that you can call a static method or reference a static variable without instantiating an object really, really, makes it clear what's going on.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top