Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

What import simplifies invocation of println?

Thread view: 
Richard - 09 Apr 2006 14:53 GMT
Hi All,

I'm a java newbie writing in the Eclipse environment over
WinXP-Pro/SP2.

I've got a bunch of System.out.println's that I'd like to write without
the "System.out" prefixes.

1.  I tried "import system.*;" but that didn't fly.  Is there some
import that helps?

2.  Is there a website where I could enter an arbitrary method name,
like println, and get a list of classes that define methods of that
name,  hopefully with an indication of the appropriate include to
access it.

Regards,
Richard
Thomas Kellerer - 09 Apr 2006 15:15 GMT
Richard wrote on 09.04.2006 15:53:
> 2.  Is there a website where I could enter an arbitrary method name,
> like println, and get a list of classes that define methods of that
> name,  hopefully with an indication of the appropriate include to
> access it.
Shouldn't Eclipse be able to do that? NetBeans allows you to search for a method
name, and will return all classes implementing that method (as long as you have
the Javadoc included in the project)

Thomas
Richard - 10 Apr 2006 03:33 GMT
Hi Thomas,

Thanks for your response.

> Shouldn't Eclipse be able to do that?

I don't know.  Chris allded to a solution,  but I think my java
installation is semi-hosed.  I'm going to start a new thread on that.

Regards,
Richard
Chris Smith - 09 Apr 2006 15:22 GMT
> I'm a java newbie writing in the Eclipse environment over
> WinXP-Pro/SP2.
[quoted text clipped - 4 lines]
> 1.  I tried "import system.*;" but that didn't fly.  Is there some
> import that helps?

You can do this in Java 1.5:

   import static java.lang.System.out;
(or import static java.lang.System.*;           )

   out.println(...);

You can't avoid the "out" since println is an instance method and you
need an object to call it on.  (i.e., you can also "println" to a
different PrintWriter or PrintStream, such as System.err or some file or
something else.)

> 2.  Is there a website where I could enter an arbitrary method name,
> like println, and get a list of classes that define methods of that
> name,  hopefully with an indication of the appropriate include to
> access it.

It's called the API documentation.  You do have that, right?  Just click
the "index" link near the top of any page.  Java doesn't have includes
or anything like them --  the compiler always knows the interface of any
class files that are accessible to it, without your taking any special
actions at all.  However, you will be able to see what package the
classes are in so you can write an appropriate import statement if you
wish to do so.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Richard - 10 Apr 2006 03:30 GMT
Hi Chris,

Thanks for your response.

> You can do this in Java 1.5:
>
>     import static java.lang.System.out;
> (or import static java.lang.System.*;           )

After updating Eclipse's properties to use Java 5.0,  it worked
great!!!

> It's called the API documentation.  You do have that, right?

My installation's screwed up.  Eclipse managed to find a 5.0 version of
javac.exe, but I suspect it used a built-in copy.  I'm going to start a
separate thread on my installation problems.

Many thanks,
Richard

Many thanks,
Richard
Chris Smith - 10 Apr 2006 04:45 GMT
> > It's called the API documentation.  You do have that, right?
>
> My installation's screwed up.  Eclipse managed to find a 5.0 version of
> javac.exe, but I suspect it used a built-in copy.  I'm going to start a
> separate thread on my installation problems.

Actually, you would need to download the API documentation separately.  
If you don't download it, you can use it from Sun's web page at
http://java.sun.com/j2se/1.5.0/docs/api/index.html

This has nothing to do with Eclipse or the JDK (aka J2SDK).  Just for
your edification (this doesn't really matter), Eclipse did not find a
copy of javac at all; it never looked, because it never uses javac.  
Instead, Eclipse ships with its own compiler that's quite distinct from
javac, and that comes with nice incremental build capabilities and a
programmatic API.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Richard - 10 Apr 2006 05:36 GMT
Hi Chris,

> Actually, you would need to download the API documentation separately.
If you don't download it, you can use it from Sun's web page at
http://java.sun.com/j2se/1.5.0/docs/api/index.html

Thanks for that.  I'm using cable to the Net,  so I prefer using
documentation that way.

>> Eclipse managed to find a 5.0 version of javac.exe, but I suspect it used a built-in copy.
>  Instead, Eclipse ships with its own compiler that's quite distinct from
javac, and that comes with nice incremental build capabilities and a
programmatic API.

That's even more than I suspected.  It's very nice to confirm my
suspicions.  I was going to post my suspicions about Eclipse compiling
Java while javac in a command window fails.

Maybe you can deduce the cause of one other phenomenon:  java -version
reports 5.0, but there's no path defined to either:

- the jre-5.0 directory or
- the java\jdk1.5.0_03\jre\bin directory

There might be a few other java.exe locations,  so I should really do
an exhaustive search to see if any of their containing folders are on
my path.

Maybe I should delecte the stand-alone jre-5.0 directory and put the
appropriate bins for Java on my path and move on.

Any comments?

Regards,
Richard

P.S.  I took a very fast look a your course design site.  I'm in the
process of putting to a web-based course on web development.  When time
permits,  I'll have to take a closer look.  Right now I'm writing my
own java presentation-creator to take disparate pages for a course I'm
goint to present and generate a web site for them with generated
navigation.
Chris Smith - 10 Apr 2006 08:54 GMT
> Maybe you can deduce the cause of one other phenomenon:  java -version
> reports 5.0, but there's no path defined to either:
>
> - the jre-5.0 directory or
> - the java\jdk1.5.0_03\jre\bin directory

The JRE on Windows installs a java.exe into your Windows system
directory, which is typically in the PATH by default.  This java.exe
will run whatever JRE is identified as the default by the registry.  
That is at least a sane thing to do... but it's generally better to add
the actual path to a JRE's bin directory at the BEGINNING of the path.  
Watch out: if you add it at the end, then the new entry won't be used
because the java.exe from the Windows system dir will be found first.

> Maybe I should delecte the stand-alone jre-5.0 directory and put the
> appropriate bins for Java on my path and move on.

Definitely don't delete your JRE.  That would cause everything to stop
working.

> P.S.  I took a very fast look a your course design site.

Ah, okay.  If you have any questions about that, feel free to send me
private email.  I just include the standard company signature on my
posts, but further discussion isn't on-topic on this newsgroup.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Richard - 10 Apr 2006 17:23 GMT
Hi Chris,

> The JRE on Windows installs a java.exe into your Windows system
directory, which is typically in the PATH by default.

Wow,  I didn't think of that.  And when I did a global search for
"java.exe",  I got swamped with results from about 30GB of backups,
which I keep in separate 100GB partition.  One of these days I'm going
to put all that stuff on an external drive which I'll only activate for
backup or retrieval.

Besides,  the invocation could have been buried in any old file and
executed by some entry in one of the startup registry entries.

Your insight helped save my sanity! :-)

> This java.exe will run whatever JRE is identified as the default by the registry.  That is at least a sane thing to do...

That's nice to learn!

> but it's generally better to add the actual path to a JRE's bin directory at the BEGINNING of the path.

Agreed.

> Definitely don't delete your JRE.

Well, I only saw your message this morning, and by that time I had
deleted ALL the java stuff,  reinstalled the JRE and then the JDK. I
added the JDK's bin to the path.  It's all working beautifully.

> If you have any questions about that, feel free to send me private email.

I sent a test message just to see if I got the address right.

Again, thanks for your insights.

Best wishes,
Richard
Alex Hunsley - 09 Apr 2006 17:56 GMT
> Hi All,
>
[quoted text clipped - 3 lines]
> I've got a bunch of System.out.println's that I'd like to write without
> the "System.out" prefixes.

Well, System is a class, out is a static member of that class (out is
PrintStream instance, to be exact), and println is a method of PrintStream.
So I suppose you could say:

 PrintStream out = System.out;

 out.println("stuff, like");

I wouldn't bother though.

> 1.  I tried "import system.*;" but that didn't fly.  Is there some
> import that helps?

You can't import member variables from another class into the current
classes namespace. And even if you could, the above would have to read
something like "import java.lang.System.*"!

> 2.  Is there a website where I could enter an arbitrary method name,
> like println, and get a list of classes that define methods of that
> name,  hopefully with an indication of the appropriate include to
> access it.

Not sure on that one...
lex

> Regards,
> Richard
Alex Hunsley - 10 Apr 2006 00:27 GMT
>> Hi All,
>>
[quoted text clipped - 20 lines]
> classes namespace. And even if you could, the above would have to read
> something like "import java.lang.System.*"!

Whoops, my bad. You can, as Lasse Reichstein Nielsen pointed out in his
reply, with 'import static'.
Richard - 10 Apr 2006 05:46 GMT
Hi Alex,

Thanks for posting.  I got to Lasse's post first, somehow, and he did a
thorough job so I'm moving forward until I stumble again.

Regards,
Richard
Lasse Reichstein Nielsen - 09 Apr 2006 20:32 GMT
> I'm a java newbie writing in the Eclipse environment over
> WinXP-Pro/SP2.
>
> I've got a bunch of System.out.println's that I'd like to write without
> the "System.out" prefixes.

Can't be done, sorry. "println" is a method on the PrintStream object
available through the public static final field "out" on the class
"java.lang.System".

You can't call a method on an object without specifiying the object
(except when the code doing it is part of the object).

You can do things to shorten the path, e.g.:

OutputStream o = System.out;
o.println("...");

or
/* near top of file */
import static System.out;
/* ... later */
... out.println("...");

> 1.  I tried "import system.*;" but that didn't fly.  Is there some
> import that helps?

"System" is not a package (nor is "system"). It's a class in the
java.lang package, and as such you don't need to import it (java.lang.*
is always imported implicitly).

In Java 5, you can import its static members using:
import static System.*;
or, less sweeping:
import static System.out;

This makes "out" available as a diret reference in the filed that
imports it.

> 2.  Is there a website where I could enter an arbitrary method name,
> like println, and get a list of classes that define methods of that
> name,  hopefully with an indication of the appropriate include to
> access it.

In Eclipse, you can try Ctrl-H to search, and choose to search for
method names.

In the standard library, you can use the API documenation:
<URL:http://java.sun.com/j2se/1.5.0/docs/api/>
The index has all methods ordered by name.

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Richard - 10 Apr 2006 04:49 GMT
Hi Lasse,

Excellent.  I got everything working except the Ctrl-H in Eclipse (It
seemed to be searching in my projects rather than externally).  I had
used the API spec years ago but forgot it.  It's perfect for my humble
needs.

I've got my java installation screwed up, but I'll start a new thread
on that question.

Best wishes,
Richard
Martin Gregorie - 09 Apr 2006 20:40 GMT
> 1.  I tried "import system.*;" but that didn't fly.  Is there some
> import that helps?

About the only way I know is via a class declaration:

public class ReportError
{
    String progName = null;

    public ReportError(String prog)
    {
        progName = prog;
    }

    public void trace(String s)
    {
        System.err.println(s);
    }

    public void error(String s)
    {
        System.err.println(progName + ": " + s);
        System.exit(1);
    }
}

Now you can use the new class to shorten what you're writing:

public MainClass
{
    ReportError r = new ReportError("MainClass");

    public static void main(String args[])
    {
        if (args.length() == 0)
            r.error("no argument supplied");
        else
            r.trace("Run starting");
        ....
        p.trace("All done");
    }
}

I use an elaboration of the above for reporting fatal errors and
outputting debugging traces.

You can get fancier if you want, such as passing a debug level to the
constructor to turn the trace() method on or off, or storing tracing
information in a circular buffer so the last few tens of trace lines
get output if a fatal condition occurs. This can be really useful for
debugging a long running, high throughput program that very occasionally
gets sent bad data as part of a sequence of inputs.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Richard - 10 Apr 2006 05:12 GMT
Hi Martin,

Thanks for the suggestions on improving the code.  I definitely had
doing something like that after I got the basic syntax for i/o,
conditionals, etc. working correctly.  I was thinking of using the
exceptions mechanism for error reporting but I haven't gotten that far
yet.

Best wishes,
Richard
Paul Hamaker - 09 Apr 2006 20:43 GMT
Richard,
if your primary motivation for question 1 is typing less, you can have
a look at templates, to be found here :
Window,
Preferences,
Java,
Editor,
Templates
there's a 'sysout' entry there, that I based my 'sop' on. So now, I
type sop , ctrl-space and voila, there you go.
--------------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com
Richard - 10 Apr 2006 05:42 GMT
Hi Paul,

I thought I posted a reply to you but it seems to have gotten lost in
the "ether."  So I'll repeat it:

Thanks for responding.

> Window |  Preferences |  Java |  Editor |  Templates | sysout

That's neat to know.  I'll have to study that when I get a chance.

Also,  I took a look at your training site.  It looks neat to, so I'll
have to return there after I finish my current project.

Best wishes,
Richard


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.