• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

passing an object to a method

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ho there one and all!

My program started off taking a parameter from the cmd line and displaying it back to the screen.

I altered this to test if a parameter was entered and if nothing was then to display a suitable message.

I then moved to two methods to display suitable messages.

Now i want to be able to write to a file the parameter passed and then to read this back and display it to the screen.

(Note each time this is run the text file is deleted and recreated!).

Now i've been told you cannot mix static methods with dynamic variables and that's fine but i don't understand !!!

I have a good understanding of OOP but when it comes to the actual doing Well then it becomes a head ache.

googled for a sample tutorial on this type of issue to no avail everything seems to brush over this (else i'm missing something VERY basic!).

program as it stands now is below.



then i get the errors below

E:\my_project\crm>javac mysimpleio.java
mysimpleio.java:75: cannot resolve symbol
symbol : method readUTF (java.lang.String)
location: class java.io.DataInputStream
thefileIn.readUTF(thefilename);
^
1 error

E:\my_project\crm>javac mysimpleio.java


Problem.... If i move the datastream decarations into main then other errors popup. see snippet below (everything else below is the same as code above.



errors

E:\my_project\crm>javac mysimpleio.java
mysimpleio.java:10: illegal start of expression
static DataOutputStream thefileOut;
^
mysimpleio.java:11: illegal start of expression
static DataInputStream thefileIn;
^
2 errors

E:\my_project\crm>

HELP
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I am not familiar with the readUTF() methods, but look in DataInput, and you find it doesn't take parameters. You will have to go through the implementing classes; I think what you are doing is setting up some sort of input stream, which takes the name of the file as a parameter to its constructor. Then I think the readUTF() goes through the file already set up, reading whatever it finds assuming it is in UTF format.

Suggest:-
  • Get rid of all "static"s except in "public static void main(String[] args)"
  • Move "String text = ""; String path = "E:\\etc\\etc":" out of the main method as fields.
  • Change "if(args.length > 0)" to "else"
  • Move the bit about setting up text into a constructor which takes String[] args as its parameter, and pass args to that constructor.
  • Move everything in the main method following this point. Most of it can be put into a new method, let's call it "go()"[LIST]I tried it and got a console printout saying "unable to find file."

  • Look here in the Java Tutorial. It says a bit about static members. In Java we don't say dynamic, even though the origin of the word "static" is from "static or dynamic." We say "instance" and the compiler says "non-static." Static members have the following characteristics:-
  • Static members are loaded into memory when the class is loaded at execution time, and stay put until (as far as I know), execution finishes.
  • Instance (non-static) members are loaded into memory when an object is created, and removed from memory when that object is no longer referenced and is deleted by the garbage collector. You could call this "dynamic memory management."
  • The static members are loaded once and once only; there is only one copy per class per JVM.
  • Static members are loaded into memory and accessible before an instance of that class is created.
  • Since there can be several instances of any class, one can never tell from a static context which instance one is trying to gain access to. So the compiler prohibits access from a static member to an instance member.
  • Access from an instance member of an object to static members of the same class is always permissible, but it is always the same static member (there is only one of each static member in memory).
  • This same static member is "shared" between all instances; if one instance alters it, all other instances find the same alteration.
  • From outside the class one ought not to say myObject.myStaticMember, but myClass.myStaticMember, using the name of the class. [This rule is not strictly enforced.] Call static members by the name of their class.
  • Beware: If you program in other languages, they have different keywords; the keyword "static" is used in C too, but it means something different. In C# it means more-or-less the same as in Java.

    Hope this helps

    CR

    [edit]minor spelling and formatting corrrections[/edit]
    [ April 11, 2007: Message edited by: Campbell Ritchie ]
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Additional:
  • As it says in the Java Tutorial reference I gave you, you cannot use the keyword "this" in a static context (nor the keyword "super," I think).
  • Look at the Math. class, which has methods which take information, calculate an answer and return it, without altering anything else in the class (eg Math.sin(PI / 2.0) gives the return 1.0 and doesn't alter anything else). If you find yourself with methods which do neither alter any information in an object, nor take any information from an object and manipulate or return it, then it is probably best to declare such methods static.

  • [edit]Change PI to PI / 2.0[/edit]
    [ April 11, 2007: Message edited by: Campbell Ritchie ]
     
    Dave Lilley
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Most of what you've said i agree with and i should have perhaps explained somethings.

    As stated in my first post, I am taking a simple task of having a console program start with a parameter OR not.

    versions...

    1. run program and display text (if arguments where given) to the screen and exit. done and moved on

    2. add a test to check for argument and if nothing display suitable message, then exit. Or display the argument that was given to the screen.
    Done and moved on

    3. use a method to do the above (namely a method to display to the screen).
    Done and moved on

    4. introduce file handling, test for the filename is present, if it is delete it, then create it again inserting the argument passed (write the argument to a file in a known location and filename), then read from that file the data inside to the screen, then exit.
    hit a snag with i/o operation (see erros at end of post)

    5. look at refining the program more (ie introduction of constructors etc).
    to be done

    all of this was so i could take baby steps to understanding how things glue together.

    I have a book here by Ivor Hilton on Beginning Java 2. Which has helped me in some understanding (and lost me on others - ps got up to end of chapter 5 defining classes then i start going glassy eyed so to speak )

    Found a tutorial that gave me this idea of this program as i kept giving up because i couldn't really relate what i was reading etc to something pratical.

    So here is the Original program i had along with the errors i got notice they all refer to the fact that te datastream "variables" for reading and writing are te issue.



    Errors i got generated when i compile (currently under linux but also under Windows too

    dave@localhost:~$ /home/dave/jdk1.6.0/bin/javac /dos/my_project/crm/mysimpleio.java
    /dos/my_project/crm/mysimpleio.java:41: non-static method ToFile(java.lang.String) cannot be referenced from a static context
    ToFile(text);
    ^
    /dos/my_project/crm/mysimpleio.java:56: non-static method FromFile(java.lang.String) cannot be referenced from a static context
    FromFile(text);
    ^
    /dos/my_project/crm/mysimpleio.java:64: cannot find symbol
    symbol : variable thefileOut
    location: class mysimpleio
    thefileOut.write(thefilename);
    ^
    /dos/my_project/crm/mysimpleio.java:67: cannot find symbol
    symbol : variable thefileOut
    location: class mysimpleio
    thefileOut.close();
    ^
    /dos/my_project/crm/mysimpleio.java:73: cannot find symbol
    symbol : variable thefileIn
    location: class mysimpleio
    thefileIn.read(thefilename);
    ^
    /dos/my_project/crm/mysimpleio.java:75: cannot find symbol
    symbol : variable thefileIn
    location: class mysimpleio
    thefileIn.close();
    ^
    6 errors
    dave@localhost:~$


    under linux i'm using Java 1.6.0 and windows it's 1.5



    please if you can assist me with the I/O issue i think i can get the constructor bit okay (well i did so when working from the book and erros i got were mostly typo's or of that ilk.

    Should i move the datastream bits down into the methods and not in the main as they are currently??

    dave.

    ps appreachiate the hints about moving code out to make it cleaner.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I dislike books which put some much into the main method; I don't think this is good object-orientation, despite examples seen in books (and even the Java Tutorial ).

    Set up an instance method to start your application off, let's call it statReading().
    Reduce your main method to the bare minimum, like this:-Move everything else you have in the main method at present into the startReading() method, except for anything required to set up the initial state of the object, which goes in the constructor.

    BTW: You are using the wrong conventions for names of classes. Use System.err rather than System.out for messages in catch blocks.

    If you are at such an early stage in your programming, you might prefer to try a few easier examples before going on to file reading and writing.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic