[Tut] Basic Java (OdinMS based - know your way through OdinMS!)

Go down

[Tut] Basic Java (OdinMS based - know your way through OdinMS!)

Post by Deagan on Mon Aug 01, 2011 5:54 am

Introduction

Why?
Hi, i've been watching the MapleStory section for a while now and came to the conclusion it's been getting worse and worse; referring to newcomers who have no clue how to work a piece of code and keep on asking questions that could easily be awnsered if some effort for learning was present. So, I decided to write a tutorial for all of them, hoping they all will take the time and effort to read this through diligently.

The Idea/Goal
My goal is to write such a tutorial, layered with chapters and subchapters, that people who release stuff can now refer to and mention what chapters or subchapters should be familiar to the reader before actually reading the thread. This way I hope to clean up this section from alot of leechers.
I hope the mods will actually adopt their system to it too, when somebody asks something that is already being mentioned in this tutorial, they should be given a warning.

NOTE: This tutorial will not teach you everything you need to know about JAVA but will be just the things that you need (in my opinion) to know your way through the general stuff of coding in Java/OdinMS.
I will not teach you how every single class operates in OdinMS.


Getting Started

Java is an Object Oriented Programming language (OOP) which basically explains itself; it's a programming language that focusses on objects. I will try to clarify in a bit.

Abstractions
To be able to code anything in java, you have to be able to think somewhat abstractly; you have to be able to see what kind of properties belong to what kind of object and be able to seperate different objects and their differences.
Example: A character (object in general) in MapleStory has 2 arms, 2 legs, a head and a body (properties). That goes for every character. Not every character is the same level though, or has the same skin color. So character 1 is not identical to character 2 but yet they belong to the object maplecharacter, while they both have their own abstractions.

Classes, Variables and Methods
To make a source ordened the properties and behaviours of the object with it's different abstractions (the properties of the characters + the things they can do) have to be saved into classes.
To save the properties of the character he uses variables and to save the behaviours he uses methods.
Variables are used to save information in and methos are pieces of code that do something (variables and methods are right now very vaguely described and more will be written about those further into this tutorial but this is purely for understanding the OOP concept).
Example; the developper uses an integer type variable (numerical) to save the level of the character and uses a method called gainMeso() to make the player gain mesos.

Objects
We can now explain what object oriented means. If you look at maplecharacter as an object, when you use it in your source you can define an object as something that has to be able to show all of the behaviours and properties that are saved in a class.
So java is all about using objects to gather data from different classes and work with them; Object Oriented Programming.


Java Elements and Structure

Declarations
Before even being able to start working with java elements (classes, variables, methods and objects) you will need to declare them.
Because of that I will show you how to declare java elements before actually describing the elements seperately.

The construction of a declaration is as follows:
Code:
<accessibility modifier(s)> <element type> <name>

You don't always just have access to every class and it's variables and methods, the traffic of java is being taken care of by accessibility modifiers.

- There are 3 accessability modifiers that take care of the visibility out of other classes:
[INDENT]Accessibility Modifiers for Visibility
  • public;[INDENT]When the modifier is used for a:
    class: the class is accessible from everywhere. (even outside of packages; the directory you added the class in)
    variable: same thing but for variables.
    method: same thing but for methods.
    [/INDENT]
  • protected;
    [INDENT]When the modifier is used for a:
    class: doesn't happen. (modifier is never used for classes)
    variable: only accessible to classes of the same package.
    method: same thing as variables but for methods.
    [/INDENT]
  • private;
    [INDENT]When the modifier is used for a:
    class: doesn't happen. (modifier is never used for classes)
    variable: only accessible to the class it is declared in.
    method: same thing as variables but for methods.
    [/INDENT]

Many people wonder why in MapleCharacter most variables are declared as private instead of just public.
Quote from [You must be registered and logged in to see this link.] (thanks Flav) :
"Deciding when to use private, protected, or public variables is sometimes tricky. You need to think whether or not an external object (or program), actually needs direct access to the information. If you do want other objects to access internal data, but wish to control it, you would make it either private or protected, but provide functions which can manipulate the data in a controlled way."

Just picture it as someone inputting data that is improper into a maplecharacter variable for now to visualise the idea of "security" in this matter. You could add a check into a method to prevent improper input.
Next to being able to control the data that's been changed securely some also state it's better for performance since it only calls the private variable upon invoking the method and doing such would save performance. (just imagine if java would have to save a variable as public to every other class instead of just one, looks like some difference somewhere but shouldn't really be a reason to use one or another)
Oh and some people think it looks cleaner to do it with methods.[/INDENT]

- There are 4 accessability modifiers that take care of the behaviour in reference to objects (I won't be looking at the "native" one but just for you to know it exists):
[INDENT]Accessibility Modifiers in Reference to other Objects
  • static;[INDENT]When the modifier is used for a:
    class: doesn't happen.
    variable: the variable is the same for every instance of the class. (i'm aware of the fact that you most of you guys don't know what an instance or reference is yet so just picture this as "if you were to make level in maplecharacter static every player's level would be the same")
    method: same thing as variables but for methods.
    [/INDENT]
  • final;
    [INDENT]When the modifier is used for a:
    class: the class is unexpandable.
    variable: the value of the variable can't be changed; constant.
    method: method can't be overrided by a subclass.
    [/INDENT]
  • abstract (take it easy Very Happy);
    [INDENT]When the modifier is used for a:
    class: class can contain abstract methods and can't be instanced as an object. It can be extended to a subclass though.
    variable: impossible.
    method: the method is an interface (I know, but I don't think I will be telling you guys about interfaces so just let this be for now), if there is 1 abstract method the class automatically needs to be abstract aswell.
    [/INDENT]

If I recall correctly it was once rumored that CelinoSEA was as stable as it was because they declared alot of things final when possible. It would have improved performance, or memory usage, I believe in such way it become really stable. I don't believe anything of this since a stable server basically means no memory leaks and deadlocks. More about those later.
[/INDENT]

- There is also 1 accessibility modifier (not taking into consideration "strictfp", "transient" and "volatile" that takes care of the behaviour of a method during runtime (running the server):

  • synchronize; the method can be invoked once at a time, aka it behaves singlethreaded instead of multithreaded.
    Be warned though, this does not always prevent deadlocks.
    In fact, I heard that using it too much creates way more of them, thus; using reentrantlock is more efficient to use when trying to prevent deadlocks. I will try my best to explain what memory leaks and deadlocks are near the end of this tutorial.


NOTE: some of these accessibility modifiers can be combined but not every combination is possible. I honestly can't be bothered to type them all out; netbeans or your IDE will let you know ^_^.
Oh, and for the name of the declaration, you will know how to give it a proper name as it is often logic sense. Next to that your IDE will tell you when you can't use a name while declaring aswell. Just make sure to stay ordered as a coder.


Elements

So now you guys actually know how to declare something we will be talking about the different elements that you can declare (classes, variables, methods and objects).
In this chapter I am assuming you generally understand what each of these elements is used for. (the maplecharacter example; "Getting Started")

NOTE: every example I use, or most of them, are written by me and it is possible there are mistakes in them.. although I hope not. Please let me know if there is false information provided anywhere in my tutorial and I will correct it. I am human.
Next to that, these codes written by me do not have to be used in any piece of code (in OdinMS-based sources) or work the same way as they do in OdinMS (especially not my maplecharacter examples);
@purpose to clarify.

Structure
To be able to understand how the pieces of code came into existence you will need to know something about the general java structure.

In the process of coding anything, it is often usefull to leave notes behind in your code so you know exactly why you added a piece of code or left it out; comments.
To leave a comment that only occupies one line of java code you simply add "//" before the comment you want to make about something and to leave a comment that takes up a couple of lines you simply open the piece of comment with "/*" and close it with "*/".
Some developpers also include information in their code about the author of a piece of code or the purpose of it ect (Javadoc comments).
It will look something like this:
Code:
/**
 *
 * @author Deagan
 * @purpose showing RaGEZONE how to add comments before using examples with them.
 */

The java developper has some rules to keep himself/herself to.
When declaring a variable or method you always add opening and closing brackets, "{" and "}", to indicate where the piece of code starts and ends. When declaring methods you also always need to add brackets, "(" and ")", to be able to give the method an input to work with. (further explained in Elements.Methods ^_^)
Last thing you should mind right now is that, unlike in JavaScript, semicolons (";") are needed after every line to let the java code know where the line ends.
You will see what I mean by this as you look at some examples I will include.

Classes
So now we know how to declare something, we want to create a class to save information about an object in so it can be used by other classes if necessary.

So let's grab the standard model for declaring elements and declare a class which will include information about a character in MapleStory:

Code:

/**
 * @Author Deagan
 * @Purpose showing RaGEZONE how to declare a class.
 * Creates a class that is accessible from every other class in every package.
 * The package simply shows in which directory the class will be saved,
 * so for this example let's save it into a folder called "examples"
 * we created in another folder called "deagansJavaTut". (case sensitive)
 */

package deagansJavaTut.examples;

// This class will also be accessible from other directories in the deagansJavaTut folder. [B]public[/B]
public class MSCharacter {
}

Normally, when declaring a class, you also declare a constructor which is the method that's invoked first when creating an object out of the class. It is not necessary to declare one but it is proper to; the compiler will create an empty constructor itself anyhow. (more about the constructor in Elements.Methods ^_^)
avatar
Deagan
Admin

Posts : 851
Join date : 2010-03-08
Age : 24
Location : Netherlands

View user profile http://drakoms.forummotion.com

Back to top Go down

Re: [Tut] Basic Java (OdinMS based - know your way through OdinMS!)

Post by Codicia on Mon Aug 01, 2011 8:26 am

Nice.
avatar
Codicia
Admin

Posts : 935
Join date : 2010-03-31
Age : 24
Location : Buenos Aires, Argentina

View user profile

Back to top Go down

Re: [Tut] Basic Java (OdinMS based - know your way through OdinMS!)

Post by BishopZProZ on Mon Aug 01, 2011 11:44 am

wow

BishopZProZ

Posts : 79
Join date : 2010-05-14

View user profile

Back to top Go down

Re: [Tut] Basic Java (OdinMS based - know your way through OdinMS!)

Post by Janniesaur on Sat Aug 06, 2011 3:05 am

Deagan is so beast .
Loveu (:
avatar
Janniesaur

Posts : 2084
Join date : 2010-04-03
Age : 77
Location : InYourPants. o _o

View user profile https://www.youtube.com/iipokies

Back to top Go down

Re: [Tut] Basic Java (OdinMS based - know your way through OdinMS!)

Post by seraphsoul on Thu Aug 11, 2011 4:01 am

watchu talkin bout willis?
avatar
seraphsoul

Posts : 1447
Join date : 2010-04-06
Age : 77
Location : Houston, TX

View user profile

Back to top Go down

Re: [Tut] Basic Java (OdinMS based - know your way through OdinMS!)

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum