java class tutorial for beginners

JAVA class tutorial for beginners: JAVA class introduction

If you are a programmer, you may know the structured programming languages like the C language.

Those programming languages separate the data and the processing in the programs:

In fact, the variables and the functions are declared sparingly: there is no relation between the variables and the functions that use them.

Then, the object oriented programming methodology appeared to deal with the problems caused by the structured programming methodology.

The object oriented programming introduces the concept of a class.

A class is considered as a type. This means that you can declare a variable whose type is the class that you have created.

A class is also a blueprint that you create before building objects of that class.

You can compare a class to a blueprint of a house. In fact, an architect creates a blueprint of a house in order to give it to a building company.

This building company will create houses (objects) that respect the blueprint created by the architect.

Let’s summarize: A class is a blueprint and the objects created from that class must respect the definition of that class (blueprint).

In fact, they must have all the attributes, all the methods and all the inner classes defined in the class definition.

You may remark that a class is like a capsule that contains a set of variables (attributes) and a set of subroutines (methods) defined in the same place and related to each others.

JAVA class tutorial for beginners: JAVA class definition

class MyFirstClass {
//attributes declaration
datatype1 attributeName1;
datatype1 attributeName2;
datatype2 attributeName3;
//methods declarations
returnType methodName1 (){
//methodName1 body
return result;
}
void methodName2(){
//methodName2 body
}
}

The JAVA class definition above defines a new class called MyFirstClass.

This JAVA class has three attributes:

  • attributeName1: of type datatype1
  • attributeName2: of type datatype1
  • attributeName3: of type datatype2

This JAVA class has also two methods:

  • methodName1: that returns a result of type returnType
  • methodName2: that does some processing and doesn’t return anything

è The variables declared inside a class are called attributes.

è The subroutines declared inside a class are called methods.

We can declare zero or more attributes in a JAVA class.

Also, we can declare zero or more methods in a JAVA class.

So, we may have a JAVA class definition like this:
class MyFirstClass {
}

This class contains nothing inside.

We can do it. But, it is not useful.

A JAVA class can contain also a definition of another class in addition to the methods and attributes. This internal class is called an inner class.

This is an advanced topic. This JAVA class tutorial for beginners will not deal with this topic. We will make a separate JAVA class tutorial for it.

We mentioned this possibility now only to make you know that we can do it.

The code of the class becomes like this:
class MyFirstClass {
//attributes declaration
datatype1 attributeName1;
datatype1 attributeName2;
datatype2 attributeName3;
//methods declarations
returnType methodName1 (){
//methodName1 body
return result;
}
void methodName2(){
//methodName2 body
}
class MyInnerClass {
}
}

The inner class in the example above is called MyInnerClass.

We can declare methods and attributes for that class. But, we didn’t to be brief.

We should also mention that the order of the declaration of the elements (attributes, methods, inner classes) of a class doesn’t matters.

In fact, we can declare the attributes at the end of the class definition and this is correct.

But, JAVA Developers used to declare them at the beginning of the class definition.

class MyFirstClass {
//methods declarations
returnType methodName1 (){
//methodName1 body
return result;
}
void methodName2(){
//methodName2 body
}
//attributes declaration
datatype1 attributeName1;
datatype1 attributeName2;
datatype2 attributeName3;
}

JAVA class tutorial for beginners: JAVA class Naming conventions

When we name a class, we should respect the camel case rule.

This rule dictates the following:

  • The name of a class always begin with an upper case letter
  • We put an upper case letter each time we start a new word in the name of a class:
    • MyFirstClass starts with an upper case letter (M).
    • This name is composed by three words (my, first, class)
    • Each time we start a new word in the name of a class, we put an upper case letter : MyFirstClass

When we name a variable, we should start it with a lowercase letter and use the camel case rule each time we encounter a new word in the name of the variable.

Examples: attributeName1, attributeName2, vehicleSpeed, etc

The java class or its elements (attributes, methods) names must begin with a letter, the dollar sign "$", or the underscore character "_".

Then you can put any sequence of letters, numbers, underscores and dollar signs.

But, we advise you to not use the dollar sign because it is used by the compiler (the dollar sign "$" should not be used).

We should pay attention because JAVA is case sensitive.

This java class tutorial for beginners arrives at its end.

We will deal with object creation in the next tutorial.

Please don’t forget to subscribe to like our How to Program Facebook page or to subscribe to our newsletter to get our latest JAVA tutorials.

To check our core java tutorial, please check this link.

See you in the next core java tutorial.









Post a Comment

Previous Post Next Post