I would like to start a post series that brings the fundamentals of Java to my audiences. For this post, I would like to focus on the basics of modifiers, classes, objects, and methods in Java! Let’s get started.
There are four different types of access modifiers in Java. A public modifier indicates that the classes, methods, and data members are accessible everywhere in the application. A private modifier denotes the methods or data members are only accessible within the class in which they are defined. A protected modifier means that the methods or data members are accessible within the same packages or within different packages under the subclasses (the class that inherits from the protected class). Lastly, a default modifier only allows the class, method, and data members to be accessed within the same package.
In Java, a class can be seen as a blueprint for instantiating objects. A class declaration can include a modifier that can either be public or default, a class keyword that is used to create a class, a class name that describes the class, a superclass AKA parent class that precedes by the keyword extends (optional), an interface that specifies the behavior of a class (optional), and a class body that defines the class. We use constructors to instantiate objects from the class. Fields are variables to indicate the state of the class and its objects and methods are used to describe the behaviors of the class and its objects.
An object in Java consists of three properties – state, behavior, and identity. The state of an object is represented by the attributes of an object. The behavior of an object is indicated by its methods. And the identity of an object is denoted by its unique name. A constructor invoked by the new operator is used to instantiate a new object based on a class; it allocates memory for a new object and returns a reference to that memory. When instantiating a new object, a class concrete class will be used as a reference for the object type. In Java, there also exist anonymous objects (instantiated but not stored in a reference variable) for immediate method calling. They are typically destroyed immediately after method calls.
Methods in Java have six components. A method consists of a modifier, the return type, method name, parameter list, exception list, and the method body. In java, the name of a method must be a verb and starts with a lowercase letter. The first letter of the following words (if any) must be in uppercase. A method return is finished when it completed all the statements in the method body or when it reaches a return statement or when an exception is thrown. When a method is called, a designated stack frame is created to store the arguments passed, local variables, and return values. Once the method call is finished, the stack frame will be deleted.
I hope you enjoyed reading this post and learned something new about Java. For the next post, I am going to talk about the constructors, static/final keywords, interfaces, and abstract classes in Java. Stay tuned!