Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A style question...

 
David Junta
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the style.jsp page, you imply (by example) that we should use curly braces like this:
void foo()
{
while ( bar > 0 )
{
System.out.println();
bar-- ;
But in your applet code, you put the opening curly brace on the same line as the class declaration or the while statement.
Are both ok?
 
paul wheaton
Trailboss
Pie
Posts: 21664
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We do not have the applet source code. It was lost.
To work on the version 2 of the applet, we had to reverse compile the applet. The reverse compiler I used generated the brace placement "the right way".
 
David Junta
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but I'm still confused.
Is this right?
-----------------------
void foo()
{
while ( bar > 0 )
{
System.out.println();
-----------------------
Or is this right?
-----------------------
void foo(){
while ( bar > 0 ) {
System.out.println();
-----------------------
Or are both ok?
 
paul wheaton
Trailboss
Pie
Posts: 21664
Firefox Browser IntelliJ IDE Java
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that when David said "your applet code", he meant one of the examlpes of code for applets in the Code Barn (or somewhere else?). When Paul read "applet code" he thought of the code for "the" applet at JavaRanch, the Rules Round-Up. So that's why these conversation fragments didnt' match up.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify: both are OK. In fact:
public void foobar() {
is much more universally used. Look in almost any Java book and you will find the braces laid out that way.
 
David Junta
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jim. You're right that my confusion was caused by the fact that I did indeed mean one of the applet examples in the Code Barn, which used a different style from the one Paul has on the style.jsp page. As Thomas points out, I've seen curly braces used almost exclusively the way I saw it in the HelloAnimThreadFirst Applet code in the Code Barn:
public void foobar() {
My point wasn't to quibble about which way is best...but rather to learn which way or ways were "acceptable" at JavaRanch. I love the site so far (my second day) and plan to continue using it regularly to learn java. Thanks, DJ
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that it is confusing that all of the code in the Code Barn doesn't match what is stated in the Style Guide. We're working to correct that ASAP.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic