Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:42:49+00:00 2026-06-17T12:42:49+00:00

When I write a class in Java, it (almost) always looks like this: public

  • 0

When I write a class in Java, it (almost) always looks like this:

public class MyClass {

    // attributes

    // constructors

    // methods:
    //   * "interesting" methods
    //   * getters and setters
    //   * toString / equals / hashCode
}

Especially, I always write all attributes at the beginning.

Is there a convention how to structure classes in Java?

I couldn’t find it in the Oracle Code Conventions. I am interested in sources (e.g. other style guides like Mozilla Coding Style or books), because I am a Java tutor and I would like to give my students more than “I have learned it that way so you should also do it like this”. And if there are reasons / sources that propose other structures within a class, I’d also be happy to know that.

edit: Although I am interested in your thoughts, I think I should stress that I expect sources. Personal experience is interesting, but I also made the experience that the structure above is common. But I want to give my students something more than the vague impression, that everybody seems to use this structure. Especially, as some of them don’t use it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:42:50+00:00Added an answer on June 17, 2026 at 12:42 pm

    You should read more carefully: The Oracle Coding conventions you linked to write in section 3.1.3:

    The following table describes the parts of a class or interface declaration, in the order that they
    should appear. See “Java Source File Example” on page 19 for an example that includes
    comments.

    1. Class/interface documentation comment (/**...*/)

      See “Documentation Comments” on page 9 for
      information on what should be in this comment.

    2. class or interface statement

    3. Class/interface implementation comment (/*...*/), if necessary

      This comment should contain any class-wide or interface-wide information that wasn’t appropriate for the class/interface documentation comment.

    4. Class (static) variables

      First the public class variables, then the protected, and then the private.

    5. Instance variables

      First public, then protected, and then private.

    6. Constructors

    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.

    That said, this convention is quite dated, and the fine print usually not followed. For instance, it seems weird to order fields by accessiblity, but methods by functionality. Indeed, I rarely see fields ordered by accessiblity in professional code. The ordering of the bullet points is adhered to quite universally in professional code, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I write a class in Java, I like to initialize the attributes which
I'd like to write singleton class for java. I found a sample program, as
I write a java class which has many getters..now I want to get all
I would like to be able to write a Java class in one package
I recently start studying JAVA, and I find this code: public class Dublicate{ public
how to write a Java class to sort 10 billion integers, assuming we can
I'm trying to write a Java class to log in to a certain website.
i am trying to write a program in java (on linux) using RandomAccessFile class
I have a short question i have wrote this in java. Old code: class
I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.