Jump to content

  • Log In with Google      Sign In   
  • Create Account

Question: Coding Conventions

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Madolite   Members   -  Reputation: 375


Posted 27 August 2014 - 08:50 PM

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:


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, 27 August 2014 - 09:11 PM.

#2 frob   Moderators   -  Reputation: 38028


Posted 27 August 2014 - 09:10 PM



I prefer reverse pyramidal structure.  Put the most important stuff (for the consumer) at the top. Put the less important implementation details at the bottom.


That means public stuff on top. Since the constructor is arguably the most important of the public stuff, it usually goes first.  Things like private constructors belong down below the public stuff.

Edited by frob, 27 August 2014 - 09:13 PM.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

#3 Madolite   Members   -  Reputation: 375


Posted 27 August 2014 - 09:14 PM

That makes a lot of sense. I'll go read up on the comments in that thread, as well. smile.png

Edited by Madolite, 27 August 2014 - 09:16 PM.

#4 mgubisch   Members   -  Reputation: 552


Posted 28 August 2014 - 01:27 AM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.