useful dynamic printline statement for current method

B

blank

just dumping this in the group if it helps.

the issue is to have a statement in a method which will always print out
the method name without having to type it in.

this works under windows.

System.out.println(new Exception().getStackTrace()[0].toString() + "");

you might find that the index [0] under other platforms is not the method
name. If not, try other values.

Not that this is just a string, so you can do other things with it than
just printLn.


Particularly useful is how this prints out in JBuilder
It gives the line number in the JBuilder message pane as a hyperlink, so
you can click on it to go to it in the editor pane.

Alternatively, unlike me, you can learn how to use the JB debugger
properly.

regards.... blankStare
 
W

Wibble

Aside from being incredibly expensive...

Why couldn't they just stick this in the language and let the compiler
define a constant.

How about printing the class name for static methods? For instances you
can do this.getClass().getName() but how do you do this
for static methods?

Both are on my wish list for java.
 
R

Ross Bamford

just dumping this in the group if it helps.

the issue is to have a statement in a method which will always print out
the method name without having to type it in.

this works under windows.

System.out.println(new Exception().getStackTrace()[0].toString() + "");

you might find that the index [0] under other platforms is not the method
name. If not, try other values.

Not that this is just a string, so you can do other things with it than
just printLn.

I like this :) I've no (current) use for it but I like it nonetheless.
You do get the source file and line number if the class has it, however,
as a sidenote.
Particularly useful is how this prints out in JBuilder
It gives the line number in the JBuilder message pane as a hyperlink, so
you can click on it to go to it in the editor pane.

Alternatively, unlike me, you can learn how to use the JB debugger
properly.

to hell with debugging.
 
T

Tor Iver Wilhelmsen

Wibble said:
How about printing the class name for static methods? For instances
you can do this.getClass().getName() but how do you do this for
static methods?

You look at the top of the source file and type the word after "public
class", adding " around it. Static members are married to their
particular class, unlike instance members which are dynamic. Well, the
methods are.
 
W

Wibble

I've had to fix countless lines where the developer followed that
procedure, and some monkey cut and paste the code to another
class, or changed the method name.
 
R

Ross Bamford

You look at the top of the source file and type the word after "public
class", adding " around it. Static members are married to their
particular class, unlike instance members which are dynamic. Well, the
methods are.

:))
 
B

blank


Ill chase this up later.
its always a question of time to learn something new (like log4j, or a
debugger) with time and tools at hand.



though as for pollutin of code with too many printlines, when I deploy I
will just replace
System.out with
// System.out
or (iflog) System.out
so it wont apply for running.
 

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
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top