{"id":13849999,"url":"https://github.com/kodecocodes/java-style-guide","last_synced_at":"2025-10-04T08:14:59.718Z","repository":{"id":32905970,"uuid":"36500819","full_name":"kodecocodes/java-style-guide","owner":"kodecocodes","description":"The official Java style guide for raywenderlich.com","archived":false,"fork":false,"pushed_at":"2017-12-05T12:01:50.000Z","size":564,"stargazers_count":152,"open_issues_count":1,"forks_count":74,"subscribers_count":24,"default_branch":"master","last_synced_at":"2025-07-12T21:37:24.798Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kodecocodes.png","metadata":{"files":{"readme":"README.markdown","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-29T11:41:42.000Z","updated_at":"2025-01-25T13:35:28.000Z","dependencies_parsed_at":"2022-09-07T11:41:59.469Z","dependency_job_id":null,"html_url":"https://github.com/kodecocodes/java-style-guide","commit_stats":null,"previous_names":["raywenderlich/java-style-guide"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kodecocodes/java-style-guide","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodecocodes%2Fjava-style-guide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodecocodes%2Fjava-style-guide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodecocodes%2Fjava-style-guide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodecocodes%2Fjava-style-guide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kodecocodes","download_url":"https://codeload.github.com/kodecocodes/java-style-guide/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodecocodes%2Fjava-style-guide/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278283621,"owners_count":25961339,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-04T02:00:05.491Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T20:00:55.412Z","updated_at":"2025-10-04T08:14:59.703Z","avatar_url":"https://github.com/kodecocodes.png","language":"Shell","readme":"# The Official raywenderlich.com Java Style Guide\n\nThis style guide is different from other you may see, because the focus is\ncentered on readability for print and the web. We created this style guide to\nkeep the code in our tutorials consistent.\n\nOur overarching goals are __conciseness__, __readability__ and __simplicity__.\n\nYou should also check out out [Swift](https://github.com/raywenderlich/swift-style-guide)\nand [Objective-C](https://github.com/raywenderlich/objective-c-style-guide)\nstyle guides too.\n\n## Inspiration\n\nThis style-guide is somewhat of a mash-up between the existing Java language\nstyle guides, and a tutorial-readability focused Swift style-guide. The language\nguidance is drawn from the\n[Android contributors style guide](https://source.android.com/source/code-style.html)\nand the\n[Google Java Style Guide](https://google-styleguide.googlecode.com/svn/trunk/javaguide.html).\nAlterations to support additional readability in tutorials were inspired by the\n[raywenderlich.com Swift style guide](https://github.com/raywenderlich/swift-style-guide).\n\n## Android Studio Coding Style\n\nIt is possible to get Android Studio to adhere to these style guidelines, via\na rather complex sequence of menus. To make it easier, we've provided a coding\nstyle that can be imported into Android Studio.\n\nFirst, clone this repository and run `install.sh`.\n\nThen, open Android Studio. To set this codestyle as the default, select\n__File \u003e Other Settings \u003e Default Settings...__:\n\n![Default Settings](resources/default_settings.png)\n\nIn __Editor \u003e Code Style__, choose the __Scheme__ to be __raywenderlich.com__:\n\n![Setting the Scheme](resources/setting_scheme.png)\n\nFrom now on, projects you create _should_ follow the correct style guidelines.\n\n\n## Table of Contents\n\n- [Nomenclature](#nomenclature)\n  + [Packages](#packages)\n  + [Classes \u0026 Interfaces](#classes--interfaces)\n  + [Methods](#methods)\n  + [Fields](#fields)\n  + [Variables \u0026 Parameters](#variables--parameters)\n  + [Misc](#misc)\n- [Declarations](#declarations)\n  + [Access Level Modifiers](#access-level-modifiers)\n  + [Fields \u0026 Variables](#fields--variables)\n  + [Classes](#classes)\n  + [Enum Classes](#enum-classes)\n- [Spacing](#spacing)\n  + [Indentation](#indentation)\n  + [Line Length](#line-length)\n  + [Vertical Spacing](#vertical-spacing)\n- [Getters \u0026 Setters](#getters--setters)\n- [Brace Style](#brace-style)\n- [Switch Statements](#switch-statements)\n- [Annotations](#annotations)\n- [XML Guidance](#xml-guidance)\n  + [XML File Names](#xml-file-names)\n  + [Indentation](#indentation-1)\n  + [Use Context-Specific XML Files](#use-context-specific-xml-files)\n  + [XML Attribute Ordering](#xml-attribute-ordering)\n- [Language](#language)\n- [Copyright Statement](#copyright-statement)\n- [Smiley Face](#smiley-face)\n- [Credit](#credits)\n\n\n## Nomenclature\n\nOn the whole, naming should follow Java standards.\n\n### Packages\n\nPackage names are all __lower-case__, multiple words concatenated together,\nwithout\nhypens or underscores:\n\n__BAD__:\n\n```java\ncom.RayWenderlich.funky_widget\n```\n\n__GOOD__:\n\n```java\ncom.raywenderlich.funkywidget\n```\n\n### Classes \u0026 Interfaces\n\nWritten in __UpperCamelCase__. For example `RadialSlider`. \n\n### Methods\n\nWritten in __lowerCamelCase__. For example `setValue`.\n\n### Fields\n\nWritten in __lowerCamelCase__.\n\nStatic fields should be written in __uppercase__, with an underscore separating\nwords:\n\n```java\npublic static final int THE_ANSWER = 42;\n```\n\nAs distasteful as it is, field naming should follow the Android source code\nnaming conventions:\n\n- Non-public, non-static field names start with an `m`.\n- Static field names start with an `s`.\n\nFor example:\n\n```java\npublic class MyClass {\n  public static final int SOME_CONSTANT = 42;\n  public int publicField;\n  private static MyClass sSingleton;\n  int mPackagePrivate;\n  private int mPrivate;\n  protected int mProtected;\n}\n```\n\n\u003e __Note:__ You can set Android Studio to follow this convention. See this SO\n\u003e link for details http://stackoverflow.com/questions/22732722/intellij-android-studio-member-variable-prefix\n\n### Variables \u0026 Parameters\n\nWritten in __lowerCamelCase__.\n\nSingle character values to be avoided except for temporary looping variables.\n\n### Misc\n\nIn code, acronyms should be treated as words. For example:\n\n__BAD:__\n\n```java\nXMLHTTPRequest\nString URL\nfindPostByID\n```\n__GOOD:__\n\n```java\nXmlHttpRequest\nString url\nfindPostById\n```\n\n## Declarations\n\n### Access Level Modifiers\n\nAccess level modifiers should be explicitly defined for classes, methods and\nmember variables.\n\n### Fields \u0026 Variables\n\nPrefer single declaration per line.\n\n__BAD:__\n\n```java\nString username, twitterHandle;\n```\n\n__GOOD:__\n\n```java\nString username;\nString twitterHandle;\n```\n\n### Classes\n\nExactly one class per source file, although inner classes are encouraged where\nscoping appropriate.\n\n\n### Enum Classes\n\nEnum classes should be avoided where possible, due to a large memory overhead.\nStatic constants are preferred. See http://developer.android.com/training/articles/memory.html#Overhead\nfor further details.\n\nEnum classes without methods may be formatted without line-breaks, as follows:\n\n```java\nprivate enum CompassDirection { EAST, NORTH, WEST, SOUTH }\n```\n\n## Spacing\n\nSpacing is especially important in raywenderlich.com code, as code needs to be\neasily readable as part of the tutorial. Java does not lend itself well to this.\n\n### Indentation\n\nIndentation is using spaces - never tabs.\n\n#### Blocks\n\nIndentation for blocks uses 2 spaces (not the default 4):\n\n__BAD:__\n\n```java\nfor (int i = 0; i \u003c 10; i++) {\n    Log.i(TAG, \"index=\" + i);\n}\n```\n\n__GOOD:__\n\n```java\nfor (int i = 0; i \u003c 10; i++) {\n  Log.i(TAG, \"index=\" + i);\n}\n```\n\n#### Line Wraps\n\nIndentation for line wraps should use 4 spaces (not the default 8):\n\n__BAD:__\n\n```java\nCoolUiWidget widget =\n        someIncrediblyLongExpression(that, reallyWouldNotFit, on, aSingle, line);\n```\n\n__GOOD:__\n\n```java\nCoolUiWidget widget =\n    someIncrediblyLongExpression(that, reallyWouldNotFit, on, aSingle, line);\n```\n\n### Line Length\n\nLines should be no longer than 100 characters long.\n\n\n### Vertical Spacing\n\nThere should be exactly one blank line between methods to aid in visual clarity \nand organization. Whitespace within methods should separate functionality, but \nhaving too many sections in a method often means you should refactor into\nseveral methods.\n\n## Getters \u0026 Setters\n\nFor external access to fields in classes, getters and setters are preferred to\ndirect access of the fields. Fields should rarely be `public`.\n\nHowever, it is encouraged to use the field directly when accessing internally\n(i.e. from inside the class). This is a performance optimization recommended\nby Google: http://developer.android.com/training/articles/perf-tips.html#GettersSetters\n\n## Brace Style\n\nOnly trailing closing-braces are awarded their own line. All others appear the\nsame line as preceding code:\n\n__BAD:__\n\n```java\nclass MyClass\n{\n  void doSomething()\n  {\n    if (someTest)\n    {\n      // ...\n    }\n    else\n    {\n      // ...\n    }\n  }\n}\n```\n\n__GOOD:__\n\n```java\nclass MyClass {\n  void doSomething() {\n    if (someTest) {\n      // ...\n    } else {\n      // ...\n    }\n  }\n}\n```\n\nConditional statements are always required to be enclosed with braces,\nirrespective of the number of lines required.\n\n__BAD:__\n\n```java\nif (someTest)\n  doSomething();\nif (someTest) doSomethingElse();\n```\n\n__GOOD:__\n\n```java\nif (someTest) {\n  doSomething();\n}\nif (someTest) { doSomethingElse(); }\n```\n\n\n## Switch Statements\n\nSwitch statements fall-through by default, but this can be unintuitive. If you\nrequire this behavior, comment it.\n\nAlway include the `default` case.\n\n__BAD:__\n\n```java\nswitch (anInput) {\n  case 1:\n    doSomethingForCaseOne();\n  case 2:\n    doSomethingForCaseOneOrTwo();\n    break;\n  case 3:\n    doSomethingForCaseOneOrThree();\n    break;\n}\n```\n\n__GOOD:__\n\n```java\nswitch (anInput) {\n  case 1:\n    doSomethingForCaseOne();\n    // fall through\n  case 2:\n    doSomethingForCaseOneOrTwo();\n    break;\n  case 3:\n    doSomethingForCaseOneOrThree();\n    break;\n  default:\n    break;\n}\n```\n\n## Annotations\n\nStandard annotations should be used - in particular `@Override`. This should\nappear the line before the function declaration.\n\n__BAD:__\n\n```java\nprotected void onCreate(Bundle savedInstanceState) {\n  super.onCreate(savedInstanceState);\n}\n```\n\n__GOOD:__\n\n```java\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n  super.onCreate(savedInstanceState);\n}\n```\n\n\n## XML Guidance\n\nSince Android uses XML extensively in addition to Java, we have some rules\nspecific to XML.\n\n### XML File Names\n\nView-based XML files should be prefixed with the type of view that they\nrepresent.\n\n__BAD:__\n\n- `login.xml`\n- `main_screen.xml`\n- `rounded_edges_button.xml`\n\n__GOOD:__\n\n- `activity_login.xml`\n- `fragment_main_screen.xml`\n- `button_rounded_edges.xml`\n\n### Indentation\n\nSimilarly to Java, indentation should be __two characters__.\n\n### Use Context-Specific XML Files\n\nWherever possible XML resource files should be used:\n\n- Strings =\u003e `res/values/strings.xml`\n- Styles =\u003e `res/values/styles.xml`\n- Colors =\u003e `res/color/colors.xml`\n- Animations =\u003e `res/anim/`\n- Drawable =\u003e `res/drawable`\n\n\n### XML Attribute Ordering\n\nWhere appropriate, XML attributes should appear in the following order:\n\n- `id` attribute\n- `layout_*` attributes\n- style attributes such as `gravity` or `textColor`\n- value attributes such as `text` or `src`\n\nWithin each of these groups, the attributes should be ordered alphabetically.\n\n\n## Language\n\nUse US English spelling.\n\n__BAD:__\n\n```java\nString colour = \"red\";\n```\n\n__GOOD:__\n\n```java\nString color = \"red\";\n```\n\n## Copyright Statement\n\nThe following copyright statement should be included at the top of every source\nfile:\n\n    /*\n     * Copyright (c) 2017 Razeware LLC\n     * \n     * Permission is hereby granted, free of charge, to any person obtaining a copy\n     * of this software and associated documentation files (the \"Software\"), to deal\n     * in the Software without restriction, including without limitation the rights\n     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n     * copies of the Software, and to permit persons to whom the Software is\n     * furnished to do so, subject to the following conditions:\n     * \n     * The above copyright notice and this permission notice shall be included in\n     * all copies or substantial portions of the Software.\n     * \n     * Notwithstanding the foregoing, you may not use, copy, modify, merge, publish, \n     * distribute, sublicense, create a derivative work, and/or sell copies of the \n     * Software in any work that is designed, intended, or marketed for pedagogical or \n     * instructional purposes related to programming, coding, application development, \n     * or information technology.  Permission for such use, copying, modification,\n     * merger, publication, distribution, sublicensing, creation of derivative works, \n     * or sale is expressly withheld.\n     *\n     * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n     * THE SOFTWARE.\n     */\n\n## Smiley Face\n\nSmiley faces are a very prominent style feature of the raywenderlich.com site!\nIt is very important to have the correct smile signifying the immense amount of\nhappiness and excitement for the coding topic. The closing square bracket ] is\nused because it represents the largest smile able to be captured using ASCII\nart. A closing parenthesis ) creates a half-hearted smile, and thus is not\npreferred.\n\nBad:\n\n    :)\n\nGood:\n\n    :]\n\n## Credits\n\nThis style guide is a collaborative effort from the most stylish\nraywenderlich.com team members:\n\n- [Darryl Bayliss](https://github.com/DarrylBayliss)\n- [Sam Davies](https://github.com/sammyd)\n- [Mic Pringle](https://github.com/micpringle)\n- [Ray Wenderlich](https://github.com/rwenderlich)\n\n","funding_links":[],"categories":["Styleguides"],"sub_categories":["Android"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkodecocodes%2Fjava-style-guide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkodecocodes%2Fjava-style-guide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkodecocodes%2Fjava-style-guide/lists"}