To declare a class, you need to write the
following:
1-Access Modifier (usually we write public).
2-class reserved keyword.
3-The class name (obeys the variable name rules).
4-The class scope (starts with open parenthesis & ends with a closing one).
1-Access Modifier (usually we write public).
2-class reserved keyword.
3-The class name (obeys the variable name rules).
4-The class scope (starts with open parenthesis & ends with a closing one).
Consider the following simple example which has 2 variables declared inside the class scope (those variables which are declared inside the class and outside the functions are called instance variables):
Example 1:
The // symbol means that everything written after this point until the end of line is a comment.
It wroth mention that instance variables better be declared with access modifiers which determines how these variables are manipulated outside the class scope.
Basically, let us talk about public and private as the most popular access modifiers:
public: is accessible from anywhere.
private: is accessible only inside the class scope.
As a convention, the class name starts with a capital letter while the variable name starts with a small letter.Both class name and variable name accepts that each following word in the name must start with a capital letter, this is preferred to improve the readability.To understand how this is done, see the following independent segments of code:
1-public class MyName // each word start with a capital letter, My, Name
2-int studentAverage; // it starts with a small letter, the 2nd
word starts with a capital letter
Consider the following example which adds access modifiers to the instance variables declared in "Example 1":
Example 2:
The executable code is usually written in main function.
Remember that we write classes to be reused, but a class cannot be useful unless we declare an object from that class, you have to declare an object to be able to call the functions inside the class and to manipulate with instance variables.
From here and so on, we will name the functions declared inside classes as methods. So, a method is a function declared inside a class.
As we've seen before, java is an object oriented programming language. So, you must have at least one class for each java program.
The main method is written inside a class. Now, let us consider the following example which modifies the last example to have main. In main, we will create 3 objects from the class MyClass and change, print their instance variables values.
Example 3:
As you can see from this example, the main is a static method. Static methods and static
variables are to be discussed later.
The main parameter list consists from "String [] agrs", it is related to arrays and strings, but we are not interested about the purpose of using it. Just write it in every main parameter list.
The result of executing the last code is:
The main parameter list consists from "String [] agrs", it is related to arrays and strings, but we are not interested about the purpose of using it. Just write it in every main parameter list.
The result of executing the last code is:
The reason to get floating points in the output is referred to the fact that a
double number consists from a true number with a floating point. So, 6.0
& -5.0 & 0.0 are
shown at the output.
As the statements of main show, the object declaration consists from:
1-Class name (MyClass here).
2-Object Name (Obeys the variable name rules).
3-new reserved keyword (To create a reference in the memory with the name of object to point at the MyClass).
4-Class name again (For reference allocation (MyClass here)).
As the statements of main show, the object declaration consists from:
1-Class name (MyClass here).
2-Object Name (Obeys the variable name rules).
3-new reserved keyword (To create a reference in the memory with the name of object to point at the MyClass).
4-Class name again (For reference allocation (MyClass here)).
5-Parameter list (Empty here (We will discuess the use of this parameter
list when we explain constructors)).
As discussed before, each object has its own copy of instance variables, so x in C1 has a different value from x in C2.
Consider that you must use the dot operator (.) to reach methods and variables inside each object, because each object in an unique copy from the class.
We didn't changed the value of any private variable and we cannot from outside the class scope. But the private variable y and the method main are in the same class.So it is possible here to change the value of this variable from main.
We can say that main is a special method used to execute the program usually. So, it is used without any calling statements.
When you declare some variable in java without initializing it with a value, the java compiler will assign it a default value of zero (0).
Classes in java are divided into two categories, pre-defined classes (already exist), user-defined classes.So, as the statements of printing in the last example show, the method of printing print is a member of class out, which is a member of class System.The general syntax of printing statement in java is:
System.out.print ();
The parameter list of method print contains what we want to print on the output
screen.
To be continued…
Arabic Version
سامر المناصرة
To be continued…
Arabic Version
سامر المناصرة
ليست هناك تعليقات:
إرسال تعليق