Introduction to Object-oriented programming

Discussion in 'Resources' started by ZNickq, Nov 15, 2011.

Thread Status:
Not open for further replies.
  1. Offline


    One thing i've noticed among most developers on bukkit is that they're not aware/ they don't make good use of objects, which is a shame, because objects are one of the best things java can offer! :D
    So, in this tutorial, i hopefully will clarify most of the questions people might have about those!

    First, let me take a real life example: a car.
    A car can do several things. It can speed up, speed down, start, stop, etc. But there are different types of cars, some better, some worse, with different speeds, for example!
    Ok, now let's take a file called "", where we'll have all the car-related stuff!
    The most important thing would probably be to create the car, and this is what we will do now:
    3. public class Car {
    5. public static Car giveCar(int speed, int horsepower) {
    6. }
    7. }

    So, see that "static" over there? it means the "giveCar" method can be accessed without actually having a car! Makes sense, right?
    Now, that method is empty, but we want it to give a car, so let's create a new Car, shall we?
    3. public class Car {
    5. Car(int speed, int horsepwr) {
    6. }
    8. public static Car giveCar(int speed, int horsepower) {
    9. Car toGive = new Car(speed, horsepower);
    10. return toGive;
    11. }
    12. }

    And this is where things get tricky...
    First, you should notice the "Car" method! But it's not a method, it's what the car needs to be instanced(created)! So, for a car to be created, let's say it needs a speed, and some horsepower!

    Then, in the static giveCar method, i create a new Car(new Car(speed,horsepower)), and return it!
    So to create a car, somebody will only have to do:
    2. Car newCar = new Car(10,100);

    And this will give him a brand new car!
    But let's say we want to actually make the car move:
    3. public class Car {
    5. private int fast, horses;
    6. private boolean engineStarted;
    8. Car(int speed, int horsepwr) {
    9. fast = speed;
    10. horses = horsepwr;
    11. }
    13. public startEngine() {
    14. engineStarted = true;
    15. }
    17. public stopEngine() {
    18. engineStarted = false;
    19. }
    21. public static Car giveCar(int speed, int horsepower) {
    22. Car toGive = new Car(speed, horsepower);
    23. return toGive;
    24. }
    25. }

    Ooh, now that's a lot of code! Let me go through all of it:
    private int stuff -> just private data which the car holds about it's state!
    private boolean engineStarted -> whether the engine is started or not!

    Now, i also added 2 methods: startEngine, and stopEngine! these set engineStarted tp true or false!
    You might notice that there's no more "static" in these methods! This is because you'll need a car (like the new car created earlier) to actually start or stop the engine!

    From here, you should be able to figure stuff out! For the sake of learning, here's a small PlayerData class, which can be stored inside a hashmap with a player, to easily get some data:
    3. class PlayerData {
    5. private Player plr;
    6. private int attacked, meters, kills;
    8. PlayerData(Player plr, int timesAttacked, int metersWalked, int kills)
    9. plr=plr;
    10. attacked = timesAttacked;
    11. meters = metersWalked;
    12. kills = kills;
    13. }
    14. public void addKill()
    15. {kills++;}
    16. public void addAttacked()
    17. {attacked++;}
    18. public void addMeter()
    19. {meters++;}
    20. public void kill()
    21. {
    22. plr.sendMessage("DIIIIE!");
    23. plr.damage(200);
    24. }
    25. public void getKills()
    26. {return kills;
    27. }
    28. //etc.
    29. }

    I hope this tutorial helped you understand objects better, if you have any problems, feel free to post here!
    Wizehh likes this.
  2. Offline


  3. Offline


  4. Offline


    My question: what is the point/advantage of using a static getCar method as opposed to just using new Car() in my code?
  5. Offline


    None, i was just demonstrating the use of static methods in a mainly non-static class ^^

    This tutorial is mainly for new plugin developers, who don't even know what an object is...+ if anyone has any problems, they can ask here!
    oh, and that website looks like shit >.>

    I know, wrote it in a rush, fixing now :)
  6. Offline

    Daniel Heppner

    Very nice.
    I've seen some good examples using balls, soccer ball, football, etc. Then you can also demonstrate casting and inheritance, both are major parts of OOP.
  7. Offline


    *puts on teaching hat*

    @ZNickq You might want to add an example of when to use static methods and explain why. If your target audience is new plugin developers they will not know this and, based on your example, will assume that classes need to have it. In fact I would remove it from this lesson and explain it somewhere else as it really isn't part of OOP, casting, and inheritance.
  8. Offline


    a static method means that it can be called statically (by referencing the class directly) rather than needing an object of the class to be instantiated (aka an instance of the class). Static variables are similar to this, in that there is only ever 1 instance of them per Class, regardless of how many times you create an instance of the Class. So referencing a static variable will always return the same reference regardless of which instance of the class it's called from (if that makes sense). This is also why your IDE will warn you that you're not referencing a static variable statically.
    ZNickq likes this.
  9. Offline


    He was just asking why he should use getCar (which returns a new Car) instead of directly new Car()...
  10. Offline


    Right, but @Sleaker did provide a fantastic explanation on the use of static variables and functions. Follow it up with a good example on when to use it and you have a good lesson for those new to programming or java (I come from pythonland so it took me a while to figure all this out!)
    ZNickq likes this.
Thread Status:
Not open for further replies.

Share This Page