A class in Java

A software program instruction is a multipel lines of code design. Program code written to solve specific problems. Think of a book having multiple chapters describing a story. Each chapter can be seen as a class in programing, way of thinking.

Class’s are a blueprint of real life object’s. When we have a Dog class, it will have a dog property and behavior. A dog will never become a cat but they are both animal.

Eg

public class NewClass { }

Summary

Visibility could be public, private or protected followed by reserved keyword ‘class’ and the name of the class. The name of the class could be anything that you want to picture.

NewClass is written in camel-case, it is a naming convention for a class. A class could be named by a single vocabulary or multiple words therefor concerning readability use CamelCase as a solution.

The opening and closing curly brackets are the body of the class, in there we will add methods and properties to the class.

Variables or fields

Variables are like vocabularies. They have their meanings inside their value and type. A software object’s state is stored in fields or variables.

Eg

public class NewClass { private String name; }

Summary

A filed or instance variable named ‘name’ with a ‘private’ visibility which makes it visible only for the class it is inside. Field ‘name’ has a datatype String so it is designed to accept a string value.

Variables should be declared before they can be used. This process involves stating the variable type and its descriptive name. Primitive data types are predefined and reserved.

Un initialized fields will have the default values as zero or null depends on the data type they are declared. It is not wrong but just generally speaking, it is considered bad programming style. Special null literal can be used as a value for all reference types but not for those primitive types.

The compiler will never assigns a default value to an uninitialized local variable. Accessing an uninitialized local variable will result a compile-time error.

Methods or functions

Single pages in a book chapter could be seen as a method or function related to the chapter they are written inside. A software object’s behavior is shown via its methods.

public class NewClass { public String getName() { return name; } public void setName(String name) { this.name = name; } } 

Summary

We have two methods, functions or behaviors in our NewClass. The getName(); methode gets the value of a field, a variable or container from its container NewClass.

The methode setName(); sets a value in the field named ‘this.name’ in another word ‘NewClass.name’. Void methode didn’t have a return value, it is just a different kind of methode.

Object’s

When it is instantiated, a class become an object.

public class NewClassObject { NewClass object; object = new NewClass(); } 

Summary

Defining the new NewClass class in another class is called instantiating.

We have seen datatypes like String, Integer and so on but those are called predefined datatypes.

We also have user defined datatypes. Since instantiating the NewClass object in another class called NewClassObject, we have now created our own datatype named NewClass.