Results 1 to 6 of 6
  1. #1
    JEltgroth is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Storing System.out to a variable

    When accepting input from the console, you can use a Scanner class as follows:

    Scanner console = new Scanner(System.in);

    I was wondering if there was some way to do the same thing with an output like this:

    ClassX varName = new ClassX(System.out);

    So then I could simply type:

    ClassX.println("");

    rather than

    System.out.println("");

    I'm doing a lot of command-line exercises and I'm looking for some way of shortening some of the code I have to type, and it would be nice not to have to type System.out every time I need it.

    I could just use JOptionPane, but I'd rather see if there's a way to do this instead.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default

    What happened when you tried exactly that?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink Static Imports

    Hey JEltgroth,

    If you feel bored typing the whole System.out.println(""), you can use the static imports in your code. They work like following,

    Java Code:
    import static java.lang.System.out;

    In this way you can directly use,
    Java Code:
    out.println("Hello Java");

    For more details on static imports, refer this link: Static-Imports.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    JEltgroth is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks goldest. That's exactly what I was looking for.

    I just wasn't sure were to look.

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Default

    You are Welcome..! :)
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default

    What I was getting at is that it's perfectly legal to do this:

    Java Code:
    PrintStream out = System.out;
    out.println("test");
    This might even be preferable, as it's more obvious what out is. With the static import, it isn't immediately obvious where the out came from (without looking in the imports). And if you have another variable named out with that static import, hilarity is going to ensue.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 11
    Last Post: 07-07-2010, 06:02 AM
  2. Replies: 3
    Last Post: 02-07-2010, 08:34 PM
  3. [SOLVED] Storing an image in the system as a file
    By sruthi_2009 in forum AWT / Swing
    Replies: 16
    Last Post: 03-23-2009, 05:18 AM
  4. Replies: 2
    Last Post: 03-31-2008, 05:11 AM
  5. Storing Data
    By Khorod in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 05:48 AM

Posting Permissions

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