What import simplifies invocation of println?

R

Richard

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
 
T

Thomas Kellerer

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
 
C

Chris Smith

Richard said:
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?

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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Alex Hunsley

Richard said:
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.

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
 
L

Lasse Reichstein Nielsen

Richard said:
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
 
M

Martin Gregorie

Richard said:
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.
 
P

Paul Hamaker

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.
 
A

Alex Hunsley

Alex said:
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.


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.*"!

Whoops, my bad. You can, as Lasse Reichstein Nielsen pointed out in his
reply, with 'import static'.
 
R

Richard

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
 
R

Richard

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
 
C

Chris Smith

Richard said:
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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Richard

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
 
R

Richard

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
 
R

Richard

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.
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.
 
R

Richard

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
 
R

Richard

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
 
C

Chris Smith

Richard said:
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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Richard

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top