Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sequencing Declarations Question

 
Marcelo Ruiz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All!
I have a simple question about sequencing declarations in a Java Class. According to the information On Sierra&Bates book, the sequence should be:

  • class comments
  • package declaration
  • import statements
  • class declaration
  • static variables
  • instance variables
  • constructors
  • methods


  • What happens if I use a static factory method? Should it go before my private constructors?
    What about static methods in general? Should I put them also before the constructors?

    Thanks!

     
    Roel De Nijs
    Sheriff
    Posts: 10230
    129
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Marcelo,

    I have first private constructor and then the static getInstance() method. In my utility classes I have first private constructor (prevent users from creating unnecessary instances) followed by all the static methods.

    Kind regards,
    Roel
     
    Fernando Franzini
    Ranch Hand
    Posts: 489
    2
    Java Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Marcelo
    That's what page 4 and 5 on javadoc convention say about that it:
    7. Methods - These methods should be grouped by functionality rather than by scope or accessibility. For example, a private class method can be in
    between two public instance methods. The goal is to make reading and understanding the code easier.

    So...Marcelo your are free to put them where your functionality tell you to put, no matter what DP you are using.
    Regard.
     
    Marcelo Ruiz
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Fernando and Roel!

    Thanks for your answers... I was feeling a little confused about the topic...

    Marcelo
     
    Anne Crace
    Ranch Hand
    Posts: 223
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just curious on one point here. I had my class comments in the wrong place (right above the class declaration), so I moved them to before the package statement. Now my comments don't show up in the javadoc, whereas they used to. I just got permission to upload so I want to clarify this. It's probably minor, but I fixed it because every little bit helps!
     
    Roberto Perillo
    Bartender
    Posts: 2271
    3
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Howdy, Anne!

    So now your classes look like the DBFileReader class?
     
    Anne Crace
    Ranch Hand
    Posts: 223
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, they do!
     
    Anne Crace
    Ranch Hand
    Posts: 223
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Disregard my last post. They do not have the comments above the class declaration, so that would appear to be the problem. I'm having a hard time finding a good example online. I guess I'm uploading tonight when I get home so I can fix this. What's a few more hours??
     
    Roberto Perillo
    Bartender
    Posts: 2271
    3
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well... when I was developing the assignment, I looked at the How to Write Doc Comments for the JavaDoc Tool and the Code Conventions for the Java Programming Language pages. Another place I also looked was the source files that come along with the JDK. They are in a .zip file in the installation directory of your JDK.
     
    Anne Crace
    Ranch Hand
    Posts: 223
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Roberto! The first one I found, but the other 2 were very helpful (especially looking at the source code from Sun ) I'll make the changes when I get home tonight and do my upload then.
     
    Roel De Nijs
    Sheriff
    Posts: 10230
    129
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I just looked at Roberto's test class and applied this structure to my own java files

    @Anne: finally submitting tonight? must be a great relief for you
     
    Anne Crace
    Ranch Hand
    Posts: 223
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I almost don't want to do the fixes because I want to get rid of it so bad, but I've already spent so much time getting the javadocs at least close to being right, I'm going to do it
     
    Roberto Perillo
    Bartender
    Posts: 2271
    3
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Howdy, Anne!

    I'd like to wish you good luck! Soon you'll also be a SCJD!!!
    Curse of the day: your score will be 400/400
     
    Anne Crace
    Ranch Hand
    Posts: 223
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Curse of the day: your score will be 400/400
    I somehow doubt this will be the case, but I think I nailed locking, though! (thanks in part to your test)
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic