Java class example : Using classes in java


The objects are data and methods bundled together into a single logical software unit. The next question that arises in our mind is how objects get into our software system and how objects get created. At this point we get introduced to the most fundamental structure in object oriented programming, the classes.

A java class is a template or prototype which defines the outline of state and behavior for all the objects belonging to that class. An instance of a class is called as object and all the objects of a class will have common state and behavior.

A java class is to an object is like a blue print is to a house. Any number of houses can be constructed from a single blue print. In a similar way many objects can be created from a single class. A class outlines the properties of an object. Objects created from the same class show similar characteristics.

The attributes and actions can be assigned only after an object is created. When an object is created then the actual instance of the entity come into existence. Hence, a java class can be called as a conceptual model of an entity which is universal and non-specific.

Java class syntax:

class identifier
{
class body
}

Here identifier specifies the name of the class. Class body consists of data members and method definitions. Curly braces surround the class body.

A class is static, whereas the data contained in an object can be dynamic. In a class, attributes are set throughout the execution of the program.

Example:

class ExampleClass
{
//Data member declaration
char ch;
int num;
double decimalnum;

void exampleMethod1()
{
System.out.println(“Hello Java”);
}
void exampleMethod2()
{
System.out.println(“Javaeschool.com”);
}
}

In the above example, ExampleClass is the class name. The class body contains 3 data members and 2 member method implementations.

Java loops : Types of loops in java


Very often we need to execute a group of statements repeatedly. Java loop statements are used to repeat the java statements number of times. Each repeated execution of statements is called as iteration. During each iteration, the value of some variable is altered and some condition is tested. This allows us to come out of the loop ultimately. If proper conditions and variable alteration is not used, you may end up in an infinite loop.

There are three types of java loops,
  1. for loop
  2. while loop
  3. do-while loop
  4. for-each

Along with loops you can use the java break and java continue statement.

Java break statement is used the break the execution of a java loop. The java break statement causes the control to pass out of the loop. Control will be transfer to the first statement after the closing curly brackets of the loop.

Java continue statement is used to skip a single iteration. When a continue statement is executed, it causes the flow of the control to the next iteration of the loop, skipping the current execution.

We can also use the nested loops and also can use the hybrid loop means like we can have a “while loop” inside “for loop” and so on.

Further to “for loop”, a new construct called for-each loop is available. This enhanced construct allows user to automatically work through the elements of a collection or an array.

Also see java for loop example, java for-each loop example, java while loop example and java do-while loop example.