1. Lesson: Language Basics
  2. Variables
    1. Naming
    2. Primitive Data Types
    3. Default Values
    4. Literals
    5. Using Underscore Characters in Numeric Literals
    6. Arrays
    7. Declaring a Variable to Refer to an Array
    8. Creating, Initializing, and Accessing an Array
    9. Copying Arrays
    10. Summary of Variables
    11. Questions and Exercises: Variables
  3. Expressions, Statements, and Blocks
    1. Expressions
    2. Statements
    3. Blocks
    4. Questions and Exercises: Expressions, Statements, and Blocks
  4. Control Flow Statements
    1. The if-then Statement
    2. The if-then-else Statement
    3. The switch Statement
    4. Using Strings in switch Statements
    5. The while and do-while Statements
    6. The for Statement
    7. The break Statement
    8. The continue Statement
    9. The return Statement
    10. Summary of Control Flow Statements
    11. Questions and Exercises: Control Flow Statements
  5. Lesson: Classes and Objects
    1. Classes
    2. Declaring Classes
    3. Declaring Member Variables
    4. Access Modifiers
    5. Types
    6. Variable Names
    7. Defining Methods
    8. Naming a Method
    9. Overloading Methods
    10. Providing Constructors for Your Classes
    11. Passing Information to a Method or a Constructor
    12. Parameter Types
    13. Arbitrary Number of Arguments
    14. Parameter Names
    15. Passing Primitive Data Type Arguments
    16. Passing Reference Data Type Arguments
    17. Objects
    18. Creating Objects
    19. Declaring a Variable to Refer to an Object
    20. Instantiating a Class
    21. Initializing an Object
    22. Using Objects
    23. Referencing an Object's Fields
    24. Calling an Object's Methods
    25. The Garbage Collector
    26. More on Classes
    27. Returning a Value from a Method
    28. Returning a Class or Interface
    29. Using the this Keyword
    30. Using this with a Field
    31. Using this with a Constructor
    32. Controlling Access to Members of a Class
    33. Understanding Instance and Class Members
    34. Class Variables
    35. Class Methods
    36. Constants
    37. The Bicycle Class
    38. Initializing Fields
    39. Static Initialization Blocks
    40. Initializing Instance Members
    41. Summary of Creating and Using Classes and Objects
    42. Questions and Exercises: Classes
    43. Questions and Exercises: Objects
  6. Nested Classes
    1. Why Use Nested Classes?
    2. Static Nested Classes
    3. Inner Classes
    4. Inner Class Example
    5. Local and Anonymous Inner Classes
    6. Modifiers
    7. Summary of Nested Classes
    8. Questions and Exercises: Nested Classes
  7. Enum Types
    1. Questions and Exercises: Enum Types
  8. Annotations
    1. Documentation
    2. Annotations Used by the Compiler
    3. Annotation Processing
    4. Questions and Exercises: Annotations
  9. Lesson: Interfaces and Inheritance
    1. Interfaces
    2. Interfaces in Java
    3. Interfaces as APIs
    4. Interfaces and Multiple Inheritance
    5. Defining an Interface
    6. The Interface Body
    7. Implementing an Interface
    8. A Sample Interface, Relatable
    9. Implementing the Relatable Interface

5.2.Declaring Classes #

You’ve seen classes defined in the following way:

class MyClass {
    //field, constructor, and method declarations

This is a class declaration. The class body (the area between the braces) contains all the code that provides for the life cycle of the objects created from the class: constructors for initializing new objects, declarations for the fields that provide the state of the class and its objects, and methods to implement the behavior of the class and its objects.
The preceding class declaration is a minimal one—it contains only those components of a class declaration that are required. You can provide more information about the class, such as the name of its superclass, whether it implements any interfaces, and so on, at the start of the class declaration. For example,

class MyClass extends MySuperClass implements YourInterface {
    //field, constructor, and method declarations

means that MyClass is a subclass of MySuperClass and that it implements the YourInterface interface.
You can also add modifiers like public or private at the very beginning—so you can see that the opening line of a class declaration can become quite complicated. The modifiers public and private, which determine what other classes can access MyClass, are discussed later in this lesson. The lesson on interfaces and inheritance will explain how and why you would use the extends and implements keywords in a class declaration. For the moment you do not need to worry about these extra complications.
In general, class declarations can include these components, in order:

  1. Modifiers such as public, private, and a number of others that you will encounter later.
  2. The class name, with the initial letter capitalized by convention.
  3. The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
  4. A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
  5. The class body, surrounded by braces, {}.
Suggest Edit