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

Using a static method to read data from text file

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I am need a little guidance here as to how I am to go about using a static method to read data from a text file. I know how to use a static method to write data to a text file, now I am wondering how to use a static method to read data from a text file. The following code is the code that read the data from the text file in the main method. Now that the code works in the main method, I am wondering how I can create a static method that does this. Thanks in advance to the person that has some ideas for me on this...

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Dave,

What's the issue you're having? Why can't you just pop this code into a method?
 
David Barry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I'm really not sure how to go about doing this. Not really where to start or what to do. I know some about static methods, just not sure how to start here...
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
you write a static method the same way you write any other method.. except you put 'static' in the signiature:

public static <return type> methodName()
{
//code goes here
}

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I have a little rule of thumb about static methods:
  • Does the method take or use any information from the state of an object of its class?
  • Does the method insert or alter any information into the state of an object of its class?
  • If you can confidently answer NO to both those questions, then you can make the method static.
  • Taking information from, or putting information into, anything passed as a parameter doesn't count. But passing a parameter of the same class probably constitutes cheating.

    I suggest you follow EFH's suggestion.

    Did I write a file-writing static method for you yesterday? Have a look at that; notice that static method takes information about the file, and what to write, as parameters. So it doesn't require any information from the state of an object of its class, nor does it alter anything about the state of any object of its class.
     
    David Barry
    Ranch Hand
    Posts: 89
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Thanks everybody. I am working on it...
     
    Campbell Ritchie
    Marshal
    Posts: 79178
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    You're welcome

    Have you noticed the serious potential problem in your 1st posting yet?
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
      Bookmark Topic Watch Topic
    • New Topic