wood burning stoves 2.0
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes code indentation - anonymous class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "code indentation - anonymous class" Watch "code indentation - anonymous class" New topic

code indentation - anonymous class

Mike Ngo
Ranch Hand

Joined: Oct 16, 2006
Posts: 89
What is the proper identation for this code?

menuItem = new JMenuItem(new AbstractAction("Exit") {
public void actionPerformed(ActionEvent e) {
[ November 04, 2006: Message edited by: Mike Ng ]
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847

Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Hi Jeroen,

Can you "support"/"prove" your indenting suggestion with a Sun standard?

I tried applying Sun's coding standards... http://java.sun.com/docs/codeconv/html/CodeConventions.doc3.html#248)
...to Mike's example and it 'aint so pretty. :-)

Yours does look nice, but you're not indenting 8 spaces...so I was wondering how you got to what you got.



SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I think Sun's standard is less than clear on this point, but my best guess is that either of these is valid:

This indents everything after the first line by 8 spaces, and then adds an additional 4 and 8 spaces of indentation to the method signature and method body, respectively.

My own personal preference, when not forced to follow Sun's standard, would be to do what Jeroen did in his first example. However I don't believe that's supported in the standard. Whether or not it's something they'd penalize you for in the SCJD, I don't know.

"I'm not back." - Bill Harding, Twister
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
I feel that Sun should not be that strict in evaluating the code conventions.
i do understand that they might need the indentation to be Java like and not like VB
what i mean is
is not allowed the preferred way is
function() {

Apart from this the basic indentation is necessary other than that it overhead. Like for example it is stated that the public and private methods areto be grouped at top etc. i do not follow that religiously. Sometimes it makes more sense to add public and its supporting private methods contiguously

-Sam Codean<br />SCJP 1.4 (98%)<br />SCJD 5.0 (87.5%)
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
The standard doesn't say you MUST use 8 spaces.
In fact it only says you SHOULD use 4 spaces (not tabs) with tabs set to 8 spaces (though you should not use them?) unless that makes for code that's hard to read in which case 8 spaces should be used instead.
I agree. Here's the link: http://aspose.com/file-tools
subject: code indentation - anonymous class
It's not a secret anymore!