import java.io.*; // import java.io package
public class Class1
{
// declare three new integer variables as class members
public int mySum1;
public int mySum2;
public int x;
// our main function
public static void main(String[] args) throws IOException
{
new Class1(); // call our Class1 constructor
}
public Class1() // our class constructor
{
// Part I///////////////////////////////
x = 5;
myFunc_1(); // call function 1
System.out.println(mySum1); // display mySum1
// Part II//////////////////////////////
// call function 2, pass in the value 5 as an argument.
myFunc_2(5);
System.out.println(mySum2); // display mySum2
// Part III/////////////////////////////
// display the returned value of function 3
System.out.println(myFunc_3(5));
}
public void myFunc_1() // define function 1
{
mySum1 = x + 5;
}
public void myFunc_2(int y) // define function 2 with an argument
{
mySum2 = y + 3;
}
public int myFunc_3(int z) // define function 3 with an integer argument
{
return z + 2;
}
}
/*
Learn to define different java class member functions.
and see how different functions handles the same type of task,
I have used different numbers so that you easier can tell them appart.
Let us take a closer look at a function definition:
----------------------------------------------------------------
public void myFunc_1()
{
mySum1 = x + 5;
}
----------------------------------------------------------------
public - mean that this member can be handled by other classes
void - means that the function is empty, it returnes no value
myFunc_1 - the name of the function
{ - start function definition
() - this means that there are no arguments
mySum1 = x + 5; - this is what our function does..
} - end function definition
----------------------------------------------------------------
Decription:
We must define what our function will do.
This function takes the number 5,
and adds it with the value from the argument called x
and stores the value in the variable mySum.
----------------------------------------------------------------
Let us take a closer look at a function call:
----------------------------------------------------------------
myFunc_1();
----------------------------------------------------------------
myFunc_1 - the name of the function
() - there is no argument passed in
; - end the function call
----------------------------------------------------------------
Decription:
You must call your function in order to "put it to work".
----------------------------------------------------------------
*/
Download Java Source Code
I hope you found this java tutorial useful!
Don't forget to mention Apron Tutorials in your References!