Java for Beginners: An Introductory Tutorial

java for beginnersJava is a vast language, complex at times and often tedious. However, setting the right foundation can help you become an expert developer in Java. Java is an object-oriented language. All of the functionalities in Java are achieved through interaction between the built-in library objects and the user defined custom objects. This article gives a brief overview of Java for beginners through code examples. The most fundamental concepts of Java have been explained in this article with the help of short code samples. So, let the fun begin.

Interested in learning more about Java? Take a course at Udemy.com.

Classes and Objects

Simplest definition of class is anything that can store data and can perform some functionality should be packaged as a class in Java. Classes are basically blue prints on which objects are based. Classes themselves are nothing and occupy no space in the memory. They provide a basic framework of action. The objects of the class actually implement that framework and perform the action specified by the class. Classes are the maps; objects are actual houses that are built on that map. And like multiple houses can be built following a particular map, multiple objects of a class can be created. The following example demonstrates this concept.

Declaring and Defining a Class

Suppose there is a conceptual entity named Car. That car has to be packaged into class. This is done in the following way:

public class Car {
public int number;
}

The above code shows how to define a class in Java. The first keyword, public defines the scope of the class which means that this class can be accessed from anywhere. Then a keyword “class” has been used. This is necessary for defining a class; finally the name of the class is defined that is Car in the above case.

This is just a declaration; actual functionality of a class is defined inside its body. The body of the class starts with an opening curly brackets and ends with a closed curly bracket. Everything inside these brackets sums up the framework of action of the class. In the above code a member variable number has been encapsulated in a class.

A class can have member variables to store data and methods to perform functionalities. Methods would be discussed in later section. Here one member variable number has been declared, the type of the variable is integer (specified by int) which means that this variable can hold an integer type data. The scope is defined by the keyword public, which means that this member variable can be accessed anywhere via the object of car class.

Instantiating a Class and Creating an Object

Creating an object of a particular class (Also known as instantiating a class) is done by calling a constructor of a class. A constructor is a method (Methods have been discussed in the next section) which has no return type and has the same name as the name of the Class which it instantiates.

For instance to create objects of Car class, the following code snippet can be used:

Car c1 = new Car();
Car c2 = new Car();
Car c3 = new Car();

c1.number = 6644;
c2.number = 4125;
c3.number = 7486;
System.out.println (c1.number +" " + c2.number + " "+ c3.number);

To create an object of the class, the class name is used followed by the name of the object and on the right side of the equal to the constructor of the class is called. For instance in the above code three objects of class Car have been instantiated. They have been named c1, c2 and c3. One class can have virtually unlimited number of objects but that will result in memory leak, so it’s never recommended. Also, an object can have any name. When a class is defined, no space in memory is reserved but when the object is instantiated via a constructor, the space is reserved in the memory for the object.

To access public member variables of a class via an object, a “.” operator is used. For instance, to access he number variable of the c1 object, the syntax used will be c1.number. In the above example, the number variable of all the three Car objects have been assigned a random value. Then using System.out.println methods, these numbers have been displayed on the console output.

Check out this course at Udemy.com for more interesting Java tutorials

Declaring Methods and Functions

Classes can store data and perform functions. Data is stored via the member variables while functionalities are performed via methods. Logically, methods can be thought of as an act that an object can perform. For instance, the concept of Car class can be extended to include methods. A Car can be started, it can be stopped it can be turned right, turned left and so on. All of these functionalities are candidate of being packaged as methods in Java.

The following example demonstrates this concept. Modify the Car class in the last example as follows:

public class Car {
public intnumber;
public void startCar()
{
System.out.println("Car has been started");
}
public int returnNumber()
{
returnnumber;
}
public void setNumber(int num)
{
number = num;
}
}

In the above code, a method named startCar has been added after the member variable number. A method has a return type that is void for startCar method, which means that this method will not return any value to calling function.

The second method is the returnNumber method. The return type of this method is integer type. This method would return the value of the member variable number. Finally the third method has been named setNumber, this method takes one parameter of type integer and stores this integer value in the member variable number.

Calling a Method

Calling a method is similar to accessing a member. This is done by writing an object name followed by “.” Operator and the method name. It is important to be noted that the parameters in the method call must match the parameters of the method. For instance if the setNumber method of the Car class is being called, an integer value has to be passed in the method call. The following example demonstrates this concept.

public class MyClass {
public staticvoid main(String[] args) {
Car c1 = new Car();
c1.startCar();
c1.setNumber(4521);
int num = c1.returnNumber();
System.out.println (num);
} }

For more detailed Java tutorials, take a course at Udemy.com.