Results 1 to 4 of 4

Thread: Passing Object references?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Passing Object references?

    Does anyone know how i could go about passing in a reference
    to an object? I want to call the static method without creating an instance of this class, so when i call the FileGrabber method
    i just have to use the class name. I not sure how i should go about doing this without having to define a constructor for this class. Any suggestions?

    Code:
    import java.io.*;
      
        public class FileGrabber{
         
        private static FileReader fr;
        private static String filepath; 
      
       static void setFileReader(JTextField jtf){ want to pass in reference to a JTextField. 
        try{ // path for file to be encrypted. 
    
        filepath = jtf.getText();
        fr = new FileReader(filepath); 
        
     
       }catch(IOException e ){System.err.println(e);}
       } 
          
        }

  2. #2
    VirtuallyVB
    Guest

    Thumbs up

    Does anyone know how i could go about passing in a reference
    to an object?
    fr = new FileReader(filepath);
    does this. "filepath" is an object reference (a reference to an object {of type String}).

    I had to add
    import javax.swing.*;
    to your FileGrabber source code to use JTextField.

    But I tested it with
    import javax.swing.*;
    public class Tester{
    public static void main(String[] args){
    T.setFileReader(new JTextField("hello.txt"));
    System.out.println("The file is " + T.getFP());
    }
    }

    You could replace
    T.setFileReader(new JTextField("hello.txt"));
    with
    JTextField myJTF = new JTextField("hello.txt");
    T.setFileReader(myJTF);

    and in this case, myJTF is your reference to an object.

    Oh, I also added
    static String getFP(){
    return filepath;
    }
    to your FileGrabber class.

    What I'd like to know is why the execution of Tester.class doesn't terminate as coded.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Im sorry, i should have given another piece of the
    code. But what im actually doing is passing an object
    reference to the method like you pointed out.

    JTextField jtf = new JTextField();
    FileGrabber.setFileReader(jtf);

    I think the problem was me not including import java.swing.*;
    Im making a small encryption/decryption program just for the fun of it. It got me thinking about our lengthy discussion on histograms . The GUI is almost finished, it just has to be fine tuned alittle. Thanks.

    import java.io.*;
    import javax.swing.*;

    public class FileGrabber{

    private static FileReader fr;
    private static String filepath;

    static void setFileReader(JTextField jtf){
    try{ // path for file to be encrypted.

    filepath = jtf.getText();
    fr = new FileReader(filepath);

    }catch(IOException e ){System.err.println(e);}
    }

    }

  4. #4
    VirtuallyVB
    Guest

    Red face

    And I wasn't exactly clear that I renamed your class to "T".

Posting Permissions

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



Click Here to Expand Forum to Full Width