This training course introduces you to the basic principles of creating Java classes. We’ll review in depth the coding requirements to create a new Java class, detailing things like constructors, fields, methods, instance variables vs static variables, etc.
Learning Objectives
- Be able to code a simple Java class
- Understand instance variables and static variables
- Be able to create object instances
- Understand the differences between Primitives vs Object References
- Be able to Implement the main method to create an instance of the defined class
- Write a class with accessor methods to read and write private instance variables
- Write a constructor to initialize an instance with data
- Write a constructor that calls other constructors of the class to benefit from code reuse
Prerequisites
- A basic understanding of software development
- A basic understanding of the software development life cycle
Intended Audience
- Software Engineers interested in learning Java to develop applications
- Software Architects interested in learning Java to design applications
- Anyone interested in basic Java application development and associated tooling
- Anyone interested in understanding the basics of the Java SDK
Okay, welcome back. In this lecture, we'll take a look at writing and adding methods to a class. The key objectives of this lecture will be for you to write a class with existing methods to read and write private instance variables, write a constructor to initialize an instance with data, write a constructor that calls other constructors of the class to benefit from code reuse, and understand how to use the this keyword to distinguish local variables from instance variables. Instance methods define an object's behavior. A method in Java is equivalent to a function, procedure or sub routine in other languages. The key components of a method are modifier, such as public or private, a return type, which may be any class, interface, primitive type, or void, the method's name, input parameters, and finally the method's body or implementation. A method may have zero or more input parameters. When you create a method, you declare the parameters which you wish to pass into the method. Each parameter is declared by stating its type followed by the parameter's name. For example, in the setName method here, we declare a single parameter named name of type string. Multiple input parameters are separated with a comma. When the method body is executed, each formal argument or input parameter becomes a local variable within the scope of the method. A method within Java must have zero or one return values. The return value may be a primitive data type. For example, Boolean or int. Or an object, for example string person employee. Or if there is no return value, it may return void. The return type is declared after the access modifier and prior to the method name.
The return statement is usually located as the last statement within a method and is the mechanism used to return values from a method. Rather than invent new names for methods, method overloading can be used when the same operation has different implementations. In the example shown here, we have two implementation of the setSalary method. The first has a single input parameter of type int, whereas the second has a single input parameter of type string. The rules for implementing method overloading are two or more methods within a class share the same name and have a different set of parameter types. The method signature must be unique. There it is. It must be defined by method name and number or type of parameters. And the return type and exceptions are not considered part of the signature. Now it is important to understand the distinction between method overloading versus method overriding. Overloading requires, as mentioned, different parameters but the same method name. The parameters must differ, either in type, order, or number. Whereas, overriding requires the same signature, name and parameters and the same return type and that the original method is inherited from its super class. At runtime, the JVM will choose the correct overloaded method to execute by inspecting the input parameter types in the method being called. Let's now review constructors. A constructor is a special method defined within the class. A developer may provide one or more constructors to the class to control the initialization of the object instances of that class. A constructor is a method with the same name as the class. Unlike regular methods, it does not have a defined return type. Fields of a class have default values that are used when an instance of the class is created. The constructor can change this by explicitly initializing the instance variables. In most cases, this is the primary function of the constructor. Each class may have zero or more constructors. If a class has no constructor, Java provides a default constructor which does nothing. It is not necessary to define a constructor for a class. As mentioned, if no constructor is defined the compiler will generate a constructor that takes no arguments. Knowing this fact is not all that critical for a developer learning Java. However, there is a case where this implicit behavior can cause a problem. If you create a class with no constructor and later use it by instantiating it using the default constructor, it will work because the compiler implicitly gave you the no argument constructor.
If you later add a constructor that takes arguments, the compiler no longer implicitly creates the no argument constructor for you and the code that instantiates the class using the no argument constructor will not compile. In the object oriented programming world, code resusabilty is the key. You want to reuse existing code as much as possible. The same holds true for constructors. If there is a constructor that can be used by another constructor, then use it. This makes maintenance much simpler. In the example shown here, the first employee constructor takes single input parameter of type string, calls the invocation of the second constructor passing in a default age parameter. Okay, consider the following questions to test yourself on the content we have just reviewed. One, two access modifiers are public and private. Two, a method can return an object reference, a primitive or void. Three, methods and constructors can be written with or without parameters. Four, multiple methods and constructors can be written with the same name provided their argument types are different. This is overloading. Five, a constructor can invoke another constructor using this, provided as the first line of the calling constructor. Six, the this keyword specifies a name defined in the class and is used to distinguish fields from method variables. Seven, yes, the methods are said to be overloaded if they share the same name but different parameters.
Jeremy is a Content Lead Architect and DevOps SME here at Cloud Academy where he specializes in developing DevOps technical training documentation.
He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 25+ years. In recent times, Jeremy has been focused on DevOps, Cloud (AWS, Azure, GCP), Security, Kubernetes, and Machine Learning.
Jeremy holds professional certifications for AWS, Azure, GCP, Terraform, Kubernetes (CKA, CKAD, CKS).