Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Java Methods, Classes and Objects
11-23-2009, 07:24 AM
Post: #1
Java Methods, Classes and Objects
This is almost the same as in C++, but it has some differences. C# took this part from Java as i saw.
First let's start with classes. Classes are groups of codes, like in example :
Code:
class YourClass
{
    //your code goes here
}
So here you have class called "YourClass".
Inside of classes you have methods and they are all in Java "jar" file.
So structure of jar file should look like :

[Image: 20180_structureofjarfile.jpg]

Now methods, that is the same as functions and procedures in Pascal, but they are called methods in Java.
So let's look at the syntax :
Code:
(public/private) (static) (void/int/String/OBJECT) NAME (ARGUMENTS)
{
   //code goes here
}
Now we are going to write an example of one class with methods that write something on to screen :
Code:
class Example {
    public void method1()
   {
        System.out.println("Hi, I'm in the first method!");
    }
    public void method2()
    {
        System.out.println("Hi, I'm in the second method!");
    }
    //And now let's try the INT return
    public int method3()
   {
        return 3; //returns the number 3
    }
    //All codes start from the MAIN method, so lets make one to do this working :D
    public static void main(String[] args)
    {
         //String[] args is a variable that contains the  arguments line, splitted by spaces so... put it but don't use it until you know what it is ^^
         method1();         //Calls method1 and print it
         method2();         //Calls method2 and print it
         int number = method3();  //Set the var "number" with the return of method3
         System.out.println("The last method is: "+number); //Method3 printer :P
    }
}
Objects are like records in Pascal, so those are groups of different variables in one variable (object). Let's say that we have class called "Man" so man has idnumber, height and weight.
Let's see :
Code:
class Man
{
int idnumber,height,weight;
Man (int idnumber, int height, int weight) {
this.idnumber=idnumber;
this.height=height;
this.weight=weight;
}
}
Now when we have created object that is "Man" and he has it's idnumber, height and weight, now we can use that group of 3 variables in the program :
Code:
class Man
{
int idnumber,height,weight;
Man (int idnumber, int height, int weight) {
this.idnumber=idnumber;
this.height=height;
this.weight=weight;
}
}
    public int getidnumber() { return idnumber; }
    public int getheight() { return height; } //returns values
    public int getweight() { return weight; }
    public int changeidnumber(int tyres) { this.idnumber = idnumber; }
}

class Main {
    public static void main(String[] args) {
        Man John = new Man(20,185,80); //declaring new man John
       //printing certain values on to screen
        System.out.println("IdNumber: "+John.getidnumber());
        System.out.println("Height: "+John.getheight());
        System.out.println("Weight: "+John.getweight());
        John.changeidnumber(35); //change idnumber
        System.out.println("IdNumber after changing: "+John.getidnumber()); //look, idnumber is changed
    }
}
I think that this helps. This is basic syntax in Java.

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: