Skip to main content
OCA Java Programmer 1Z0-803

Building classes in Java 7 with constructors and fields

By July 27, 2013September 12th, 2022No Comments

From out previous lesson we has seen that we can organize our code into packages and import classes from other packages with the import statement. Both the package and the import statement of optional depending on the classes you access and how you have organized your code. After these statements we would define the class in our java file. The class mates the name of the java file without the java extension.

If our class is called ShowSystem then this will be defined in a java source file

We can view how a java file should go together with pseudo-code:

[modifier] class ClassName ]extends SuperClass] [implements Interface] {

Access Modifier

This can be left blank in which case the default level of package is used. Possible values are:

  • public
  • protected
  • package (default)
  • private

If excluded then the level package will allow access by other classes with the same package, private just to the class itself, protected will allow access from the package classes and related classes and public all classes.


Identifies this object as a class


Is written in camel case with the first letter in upper case.


This allows us to extends and existing class so that we can inherit methods and properties set in the parent class. The parent class is known as the super class ; if omitted then it is implied inheritance from the class Object. A class can extend from just one super class


Similar to extends the implements key word allows us to make use of Interfaces; Interfaces usually provide command methods across classes and a single class can implements multiple interface.

Within the braces we defines the elements of the class itself. We should defines them in the following order:


These fields or what are know in some circles as data members allows us to set fields or properties of the class. For example a class Car may have a field wheels and we can set the value of wheels to 4.


Constructors can allow for fields within the class to be populated at the same time that an instance of the object is created, or, during object instantiation. They enforce arguments to be passed through to the object when it is created in a similar way to methods, unlike methods a constructor never return values.


Methods are the verbs or the doing elements of a class. Unlike constructors they can return values and must include either the date type of the return value or the key word void if nothing is to be returned by the method. Methods will be covered in more details in another lesson.

Take the time to run through the video demo to see live code implementing classes, fields and constructors.