Sign in to follow this  
Madolite

Question: Coding Conventions

Recommended Posts

Madolite    393

Hey guys, I'm trying to look up the various conventions for writing classes in Java (and beyond). For anyone equally new to programming, I've been looking at the various coding standards found @ https://ace.apache.org/dev-doc/coding-standards.html

 

My Question:
For the pros out there, I'm trying to get confirmed where (in Java) I should place constructors. Is it typical to group them all up before or after normal methods? I've seen a lot of people start with constructors.

Here's what I'm thinking right now:

/** 
  * JAVADOC
  *
  */

package gTLD.subdomain.application.package.subpackage.*;

import external package.subpackage.*;
    
// Class, Interface, Enum and Annotation names: nouns with first letter per word capitalized.
public MyClass extends SuperClass implements Interface {

    public static void main(String[] args) {
    }

    private static final int CONSTANTINE = 123; // All final variables at top of list.
    private static String m_goldMember = null; // Other fields. Notice the m_

// Recommend one declaration per line. Encourage commenting and cause less confusion.
// All instance variables at bottom of list.

    private int x = 0; // x of something
    private int y = 0; // y of something

/* Methods: Verbs with first letter lowercase.
 * Vars used and declared within methods must always be initialized explicitly.
 * Declare all local vars just before needed.
 */

    Constructor (x, y, z) {
    }

    method (x, y, z) {
    }

    String toString() {
        return stringConcat; // stringConcat: whichever variables concatonated to create the string;
    }
}

Feel free to comment on how you personally organize things and what you think is good and bad (e.g. about my code here).

Thanks in advance.

Edited by Madolite

Share this post


Link to post
Share on other sites
rave3d    691

I normally organize my classes as follows:

 

public static member

public static methods

public members

public constructors

public methods

 

same for protected

same for private

 

With this method the things most important do the user of your class are on top

 

Another method I've seen and found not to bad:

 

everything static

all methods in oder public, protected. private

all members in order public, protected, private

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this